Spaces:
Build error
Build error
fix: improved code rules
Browse files
backend/functions/src/cloud-functions/crawler.ts
CHANGED
|
@@ -132,7 +132,10 @@ export class CrawlerHost extends RPCHost {
|
|
| 132 |
}
|
| 133 |
|
| 134 |
getTurndown(noRules?: boolean | string) {
|
| 135 |
-
const turnDownService = new TurndownService(
|
|
|
|
|
|
|
|
|
|
| 136 |
if (!noRules) {
|
| 137 |
turnDownService.addRule('remove-irrelevant', {
|
| 138 |
filter: ['meta', 'style', 'script', 'noscript', 'link', 'textarea'],
|
|
@@ -179,6 +182,30 @@ export class CrawlerHost extends RPCHost {
|
|
| 179 |
return `[${fixedContent}](${fixedHref}${title || ''})`;
|
| 180 |
}
|
| 181 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
return turnDownService;
|
| 184 |
}
|
|
|
|
| 132 |
}
|
| 133 |
|
| 134 |
getTurndown(noRules?: boolean | string) {
|
| 135 |
+
const turnDownService = new TurndownService({
|
| 136 |
+
codeBlockStyle: 'fenced',
|
| 137 |
+
preformattedCode: true,
|
| 138 |
+
} as any);
|
| 139 |
if (!noRules) {
|
| 140 |
turnDownService.addRule('remove-irrelevant', {
|
| 141 |
filter: ['meta', 'style', 'script', 'noscript', 'link', 'textarea'],
|
|
|
|
| 182 |
return `[${fixedContent}](${fixedHref}${title || ''})`;
|
| 183 |
}
|
| 184 |
});
|
| 185 |
+
turnDownService.addRule('improved-code', {
|
| 186 |
+
filter: function (node: any) {
|
| 187 |
+
let hasSiblings = node.previousSibling || node.nextSibling;
|
| 188 |
+
let isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings;
|
| 189 |
+
|
| 190 |
+
return node.nodeName === 'CODE' && !isCodeBlock;
|
| 191 |
+
},
|
| 192 |
+
|
| 193 |
+
replacement: function (inputContent: any) {
|
| 194 |
+
if (!inputContent) return '';
|
| 195 |
+
let content = inputContent;
|
| 196 |
+
|
| 197 |
+
let delimiter = '`';
|
| 198 |
+
let matches = content.match(/`+/gm) || [];
|
| 199 |
+
while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`';
|
| 200 |
+
if (content.includes('\n')) {
|
| 201 |
+
delimiter = '```';
|
| 202 |
+
}
|
| 203 |
+
|
| 204 |
+
let extraSpace = delimiter === '```' ? '\n' : /^`|^ .*?[^ ].* $|`$/.test(content) ? ' ' : '';
|
| 205 |
+
|
| 206 |
+
return delimiter + extraSpace + content + (delimiter === '```' && !content.endsWith(extraSpace) ? extraSpace : '') + delimiter;
|
| 207 |
+
}
|
| 208 |
+
});
|
| 209 |
|
| 210 |
return turnDownService;
|
| 211 |
}
|
thinapps-shared
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
Subproject commit
|
|
|
|
| 1 |
+
Subproject commit 5939c7091985706bebe7d1d83591430426b292c8
|