Skip to content

Commit 144b4f8

Browse files
committed
update: refactor
1 parent 35a7261 commit 144b4f8

File tree

4 files changed

+290
-78
lines changed

4 files changed

+290
-78
lines changed

src/plugins/search/component.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function doSearch(value) {
5353
<div class="matching-post" aria-label="search result ${i + 1}">
5454
<a href="${post.url}">
5555
<p class="title clamp-1">${post.title}</p>
56-
<p class="content clamp-2">${post.content}</p>
56+
<p class="content clamp-2">...${post.content}...</p>
5757
</a>
5858
</div>
5959
`;

src/plugins/search/markdown-to-txt.js

+188-52
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,197 @@
11
/**
2-
* This is a modified version of the
3-
* [markdown-to-txt](https://www.npmjs.com/package/markdown-to-txt) library.
2+
* This is a function to convert markdown to txt based on markedjs v13+.
3+
* Copies the escape/unescape functions from [lodash](https://www.npmjs.com/package/lodash) instead import to reduce the size.
44
*/
55
import { marked } from 'marked';
6-
import { escape, unescape } from 'lodash';
7-
const block = text => text + '\n\n';
8-
const escapeBlock = text => escape(text) + '\n\n';
9-
const line = text => text + '\n';
10-
const inline = text => text;
11-
const newline = () => '\n';
12-
const empty = () => '';
13-
14-
const TxtRenderer = {
15-
// Block elements
16-
code: escapeBlock,
17-
blockquote: block,
18-
html: empty,
19-
heading: block,
20-
hr: newline,
21-
list: text => block(text.trim()),
22-
listitem: line,
23-
checkbox: empty,
24-
paragraph: block,
25-
table: (header, body) => line(header + body),
26-
tablerow: text => line(text.trim()),
27-
tablecell: text => text + ' ',
28-
// Inline elements
29-
strong: inline,
30-
em: inline,
31-
codespan: inline,
32-
br: newline,
33-
del: inline,
34-
link: (_0, _1, text) => text,
35-
image: (_0, _1, text) => text,
36-
text: inline,
37-
// etc.
38-
options: {},
6+
7+
const reEscapedHtml = /&(?:amp|lt|gt|quot|#(0+)?39);/g;
8+
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
9+
const htmlUnescapes = {
10+
'&amp;': '&',
11+
'&lt;': '<',
12+
'&gt;': '>',
13+
'&quot;': '"',
14+
'&#39;': "'",
3915
};
4016

41-
/**
42-
* Converts markdown to plaintext using the marked Markdown library.
43-
* Accepts [MarkedOptions](https://marked.js.org/using_advanced#options) as
44-
* the second argument.
45-
*
46-
* NOTE: The output of markdownToTxt is NOT sanitized. The output may contain
47-
* valid HTML, JavaScript, etc. Be sure to sanitize if the output is intended
48-
* for web use.
49-
*
50-
* @param markdown the markdown text to txtify
51-
* @param options the marked options
52-
* @returns the unmarked text
53-
*/
54-
export function markdownToTxt(markdown, options) {
55-
const unmarked = marked(markdown, { ...options, renderer: TxtRenderer });
17+
function unescape(string) {
18+
return string && reHasEscapedHtml.test(string)
19+
? string.replace(reEscapedHtml, entity => htmlUnescapes[entity] || "'")
20+
: string || '';
21+
}
22+
23+
const reUnescapedHtml = /[&<>"']/g;
24+
const reHasUnescapedHtml = RegExp(reUnescapedHtml.source);
25+
const htmlEscapes = {
26+
'&': '&amp;',
27+
'<': '&lt;',
28+
'>': '&gt;',
29+
'"': '&quot;',
30+
"'": '&#39;',
31+
};
32+
33+
function escape(string) {
34+
return string && reHasUnescapedHtml.test(string)
35+
? string.replace(reUnescapedHtml, chr => htmlEscapes[chr])
36+
: string || '';
37+
}
38+
39+
function helpersCleanup(string) {
40+
return string && string.replace('!>', '').replace('?>', '');
41+
}
42+
43+
const markdownToTxtRenderer = {
44+
space() {
45+
return '';
46+
},
47+
48+
code({ text }) {
49+
const code = text.replace(/\n$/, '');
50+
return escape(code);
51+
},
52+
53+
blockquote({ tokens }) {
54+
return this.parser?.parse(tokens) || '';
55+
},
56+
57+
html() {
58+
return '';
59+
},
60+
61+
heading({ tokens }) {
62+
return this.parser?.parse(tokens) || '';
63+
},
64+
65+
hr() {
66+
return '';
67+
},
68+
69+
list(token) {
70+
let body = '';
71+
for (let j = 0; j < token.items.length; j++) {
72+
const item = token.items[j];
73+
body += this.listitem?.(item);
74+
}
75+
76+
return body;
77+
},
78+
79+
listitem(item) {
80+
let itemBody = '';
81+
if (item.task) {
82+
const checkbox = this.checkbox?.({ checked: !!item.checked });
83+
if (item.loose) {
84+
if (item.tokens.length > 0 && item.tokens[0].type === 'paragraph') {
85+
item.tokens[0].text = checkbox + ' ' + item.tokens[0].text;
86+
if (
87+
item.tokens[0].tokens &&
88+
item.tokens[0].tokens.length > 0 &&
89+
item.tokens[0].tokens[0].type === 'text'
90+
) {
91+
item.tokens[0].tokens[0].text =
92+
checkbox + ' ' + item.tokens[0].tokens[0].text;
93+
}
94+
} else {
95+
item.tokens.unshift({
96+
type: 'text',
97+
raw: checkbox + ' ',
98+
text: checkbox + ' ',
99+
});
100+
}
101+
} else {
102+
itemBody += checkbox + ' ';
103+
}
104+
}
105+
106+
itemBody += this.parser?.parse(item.tokens, !!item.loose);
107+
108+
return `${itemBody || ''}`;
109+
},
110+
111+
checkbox() {
112+
return '';
113+
},
114+
115+
paragraph({ tokens }) {
116+
return this.parser?.parseInline(tokens) || '';
117+
},
118+
119+
table(token) {
120+
let header = '';
121+
122+
let cell = '';
123+
for (let j = 0; j < token.header.length; j++) {
124+
cell += this.tablecell?.(token.header[j]);
125+
}
126+
header += this.tablerow?.({ text: cell });
127+
128+
let body = '';
129+
for (let j = 0; j < token.rows.length; j++) {
130+
const row = token.rows[j];
131+
132+
cell = '';
133+
for (let k = 0; k < row.length; k++) {
134+
cell += this.tablecell?.(row[k]);
135+
}
136+
137+
body += this.tablerow?.({ text: cell });
138+
}
139+
140+
return header + ' ' + body;
141+
},
142+
143+
tablerow({ text }) {
144+
return text;
145+
},
146+
147+
tablecell(token) {
148+
return this.parser?.parseInline(token.tokens) || '';
149+
},
150+
151+
strong({ text }) {
152+
return text;
153+
},
154+
155+
em({ tokens }) {
156+
return this.parser?.parseInline(tokens) || '';
157+
},
158+
159+
codespan({ text }) {
160+
return text;
161+
},
162+
163+
br() {
164+
return ' ';
165+
},
166+
167+
del({ tokens }) {
168+
return this.parser?.parseInline(tokens);
169+
},
170+
171+
link({ tokens, href, title }) {
172+
// Remain the href and title attributes for searching, so is the image
173+
// e.g. [filename](_media/example.js ':include :type=code :fragment=demo')
174+
// Result: filename _media/example.js :include :type=code :fragment=demo
175+
return `${this.parser?.parseInline(tokens) || ''} ${href || ''} ${title || ''}`;
176+
},
177+
178+
image({ title, text, href }) {
179+
return `${text || ''} ${href || ''} ${title || ''}`;
180+
},
181+
182+
text(token) {
183+
return token.tokens
184+
? this.parser?.parseInline(token.tokens) || ''
185+
: token.text || '';
186+
},
187+
};
188+
const _marked = marked.setOptions({ renderer: markdownToTxtRenderer });
189+
190+
export function markdownToTxt(markdown) {
191+
const unmarked = _marked.parse(markdown);
56192
const unescaped = unescape(unmarked);
57-
const trimmed = unescaped.trim();
58-
return trimmed;
193+
const helpersCleaned = helpersCleanup(unescaped);
194+
return helpersCleaned.trim();
59195
}
60196

61197
export default markdownToTxt;

src/plugins/search/search.js

+13-19
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ function escapeHtml(string) {
6060
return String(string).replace(/[&<>"']/g, s => entityMap[s]);
6161
}
6262

63-
function formatContent(text) {
64-
return escapeHtml(cleanMarkdown(ignoreDiacriticalMarks(text)));
65-
}
66-
67-
function cleanMarkdown(text) {
68-
if (text) {
69-
text = markdownToTxt(text);
70-
}
71-
return text;
72-
}
73-
7463
function getAllPaths(router) {
7564
const paths = [];
7665

@@ -146,7 +135,7 @@ export function genIndex(path, content = '', router, depth, indexKey) {
146135
index[slug] = {
147136
slug,
148137
title: path !== '/' ? path.slice(1) : 'Home Page',
149-
body: token.text || '',
138+
body: markdownToTxt(token.text || ''),
150139
path: path,
151140
indexKey: indexKey,
152141
};
@@ -162,12 +151,12 @@ export function genIndex(path, content = '', router, depth, indexKey) {
162151
token.text = getTableData(token);
163152
token.text = getListData(token);
164153

165-
index[slug].body += '\n' + (token.text || '');
154+
index[slug].body += '\n' + markdownToTxt(token.text || '');
166155
} else {
167156
token.text = getTableData(token);
168157
token.text = getListData(token);
169158

170-
index[slug].body = token.text || '';
159+
index[slug].body = markdownToTxt(token.text || '');
171160
}
172161

173162
index[slug].path = path;
@@ -211,14 +200,19 @@ export function search(query) {
211200
keywords.forEach(keyword => {
212201
// From https://github.com/sindresorhus/escape-string-regexp
213202
const regEx = new RegExp(
214-
formatContent(keyword).replace(/[|\\{}()[\]^$+*?.]/g, '\\$&'),
203+
escapeHtml(ignoreDiacriticalMarks(keyword)).replace(
204+
/[|\\{}()[\]^$+*?.]/g,
205+
'\\$&',
206+
),
215207
'gi',
216208
);
217209
let indexTitle = -1;
218210
let indexContent = -1;
219-
handlePostTitle = postTitle ? formatContent(postTitle) : postTitle;
211+
handlePostTitle = postTitle
212+
? escapeHtml(ignoreDiacriticalMarks(postTitle))
213+
: postTitle;
220214
handlePostContent = postContent
221-
? formatContent(postContent)
215+
? escapeHtml(ignoreDiacriticalMarks(postContent))
222216
: postContent;
223217

224218
indexTitle = postTitle ? handlePostTitle.search(regEx) : -1;
@@ -252,8 +246,8 @@ export function search(query) {
252246

253247
if (matchesScore > 0) {
254248
const matchingPost = {
255-
title: formatContent(handlePostTitle),
256-
content: formatContent(postContent ? resultStr : ''),
249+
title: handlePostTitle,
250+
content: postContent ? resultStr : '',
257251
url: postUrl,
258252
score: matchesScore,
259253
};

0 commit comments

Comments
 (0)