Skip to content

Commit 66a3ac6

Browse files
committed
fix(index.js,main.js): 修复由h2标题引起的offset out of bounds错误
1 parent d625ddc commit 66a3ac6

File tree

2 files changed

+15
-31
lines changed

2 files changed

+15
-31
lines changed

api/main.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,6 @@ var client = new elasticsearch.Client({
55
const express = require('express');
66
const app = express();
77

8-
function count(str) {
9-
const regex = /<em>(.*?)<\/em>/g;
10-
let len = 0;
11-
let match;
12-
13-
while ((match = regex.exec(str)) !== null) {
14-
len += match[1].length;
15-
}
16-
17-
return len;
18-
}
19-
208
app.set('port', process.env.PORT || 8000);
219

2210
app.get('/status', function(req, res) {
@@ -113,16 +101,11 @@ app.get('/', function(req, res) {
113101
results = results.hits.hits;
114102
results = results.map((e) => {
115103
let highlight = [];
116-
if (!e.highlight || (!e.highlight.content && !e.highlight.h2)) {
117-
highlight = [e._source.content.substring(0, 30)];
118-
} else if (e.highlight.content && e.highlight.h2) {
119-
const contentCount = count(e.highlight.content[0]);
120-
const h2Count = count(e.highlight.h2[0]);
121-
122-
highlight = (contentCount >= h2Count) ? e.highlight.content : e.highlight.h2;
123-
} else {
124-
highlight = e.highlight.content || e.highlight.h2;
125-
}
104+
if (!e.highlight || !e.highlight.content) {
105+
highlight = [e._source.content.substring(0, 50)];
106+
} else {
107+
highlight = e.highlight.content;
108+
}
126109
return {
127110
url: e._source.url,
128111
title: e._source.title,

webhook/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ function getContent(filename, data) {
5858
let title = lines[0] && lines[0].match(h1reg) ?
5959
lines[0].replace('# ', '') : '';
6060
traversalArticle(data['nav'], (key, value) => {
61-
if(value == filename) title = key;
61+
if (value == filename) title = key;
6262
});
63-
const h2 = lines.filter(e => e.match(h2reg)).map(e => `<h2>${e.replace(/^## /, '')}</h2>`);
63+
const h2 = lines.filter(e => e.match(h2reg)).map(e => e.replace(/^## /, ''));
6464

6565
others = others.map(e => e.replace(/^##+ /, ''));
6666

@@ -76,7 +76,7 @@ function getContent(filename, data) {
7676

7777
others.replace()
7878

79-
return [title, others, h2];
79+
return [title, others, h2.join('\n')];
8080
}
8181

8282
/**
@@ -161,12 +161,13 @@ handler.on('push', (event) => {
161161
});
162162

163163
function init() {
164-
exec(`bash build.sh`);
165-
let modified = [];
166-
const file = String(fs.readFileSync(`/home/ubuntu/OI-wiki/mkdocs.yml`));
167-
const data = YAML.parse(file.replaceAll('!!python/name:', ''));
168-
traversalArticle(data['nav'], (key, value) => modified.push(value));
169-
updateContent(modified, []);
164+
exec(`bash build.sh`, () => {
165+
let modified = [];
166+
const file = String(fs.readFileSync(`/home/ubuntu/OI-wiki/mkdocs.yml`));
167+
const data = YAML.parse(file.replaceAll('!!python/name:', ''));
168+
traversalArticle(data['nav'], (key, value) => modified.push(value));
169+
updateContent(modified, []);
170+
});
170171
}
171172

172173
init();

0 commit comments

Comments
 (0)