Spaces:
Build error
Build error
fix: potential gfm performance issue
Browse files
backend/functions/src/services/snapshot-formatter.ts
CHANGED
|
@@ -45,14 +45,37 @@ export interface FormattedPage {
|
|
| 45 |
export const md5Hasher = new HashManager('md5', 'hex');
|
| 46 |
|
| 47 |
const gfmPlugin = require('turndown-plugin-gfm');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
@singleton()
|
| 50 |
export class SnapshotFormatter extends AsyncService {
|
| 51 |
|
| 52 |
logger = this.globalLogger.child({ service: this.constructor.name });
|
| 53 |
|
| 54 |
-
gfmPlugin = gfmPlugin.
|
| 55 |
-
gfmNoTable = [
|
| 56 |
|
| 57 |
constructor(
|
| 58 |
protected globalLogger: Logger,
|
|
@@ -475,7 +498,7 @@ ${suffixMixins.length ? `\n${suffixMixins.join('\n\n')}\n` : ''}`;
|
|
| 475 |
imgDataUrlToObjectUrl?: boolean;
|
| 476 |
removeImages?: boolean | 'src';
|
| 477 |
customRules?: { [k: string]: Rule; };
|
| 478 |
-
customKeep?: Filter
|
| 479 |
}) {
|
| 480 |
const turnDownService = new TurndownService({
|
| 481 |
codeBlockStyle: 'fenced',
|
|
|
|
| 45 |
export const md5Hasher = new HashManager('md5', 'hex');
|
| 46 |
|
| 47 |
const gfmPlugin = require('turndown-plugin-gfm');
|
| 48 |
+
const highlightRegExp = /highlight-(?:text|source)-([a-z0-9]+)/;
|
| 49 |
+
|
| 50 |
+
export function highlightedCodeBlock(turndownService: TurndownService) {
|
| 51 |
+
turndownService.addRule('highlightedCodeBlock', {
|
| 52 |
+
filter: (node) => {
|
| 53 |
+
return (
|
| 54 |
+
node.nodeName === 'DIV' &&
|
| 55 |
+
node.firstChild?.nodeName === 'PRE' &&
|
| 56 |
+
highlightRegExp.test(node.className)
|
| 57 |
+
);
|
| 58 |
+
},
|
| 59 |
+
replacement: (_content, node, options)=> {
|
| 60 |
+
const className = (node as any).className || '';
|
| 61 |
+
const language = (className.match(highlightRegExp) || [null, ''])[1];
|
| 62 |
+
|
| 63 |
+
return (
|
| 64 |
+
'\n\n' + options.fence + language + '\n' +
|
| 65 |
+
node.firstChild!.textContent +
|
| 66 |
+
'\n' + options.fence + '\n\n'
|
| 67 |
+
);
|
| 68 |
+
}
|
| 69 |
+
});
|
| 70 |
+
}
|
| 71 |
|
| 72 |
@singleton()
|
| 73 |
export class SnapshotFormatter extends AsyncService {
|
| 74 |
|
| 75 |
logger = this.globalLogger.child({ service: this.constructor.name });
|
| 76 |
|
| 77 |
+
gfmPlugin = [gfmPlugin.tables, highlightedCodeBlock, gfmPlugin.strikethrough, gfmPlugin.taskListItems];
|
| 78 |
+
gfmNoTable = [highlightedCodeBlock, gfmPlugin.strikethrough, gfmPlugin.taskListItems];
|
| 79 |
|
| 80 |
constructor(
|
| 81 |
protected globalLogger: Logger,
|
|
|
|
| 498 |
imgDataUrlToObjectUrl?: boolean;
|
| 499 |
removeImages?: boolean | 'src';
|
| 500 |
customRules?: { [k: string]: Rule; };
|
| 501 |
+
customKeep?: Filter;
|
| 502 |
}) {
|
| 503 |
const turnDownService = new TurndownService({
|
| 504 |
codeBlockStyle: 'fenced',
|