From ee008ebe10a172a31228d42e92d70bba45b5fd72 Mon Sep 17 00:00:00 2001 From: "yanlong.wang" Date: Thu, 13 Jun 2024 16:27:30 +0800 Subject: [PATCH] fix: improved code rules --- .../functions/src/cloud-functions/crawler.ts | 29 ++++++++++++++++++- thinapps-shared | 2 +- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/backend/functions/src/cloud-functions/crawler.ts b/backend/functions/src/cloud-functions/crawler.ts index 31c3924..dc18bbb 100644 --- a/backend/functions/src/cloud-functions/crawler.ts +++ b/backend/functions/src/cloud-functions/crawler.ts @@ -132,7 +132,10 @@ export class CrawlerHost extends RPCHost { } getTurndown(noRules?: boolean | string) { - const turnDownService = new TurndownService(); + const turnDownService = new TurndownService({ + codeBlockStyle: 'fenced', + preformattedCode: true, + } as any); if (!noRules) { turnDownService.addRule('remove-irrelevant', { filter: ['meta', 'style', 'script', 'noscript', 'link', 'textarea'], @@ -179,6 +182,30 @@ export class CrawlerHost extends RPCHost { return `[${fixedContent}](${fixedHref}${title || ''})`; } }); + turnDownService.addRule('improved-code', { + filter: function (node: any) { + let hasSiblings = node.previousSibling || node.nextSibling; + let isCodeBlock = node.parentNode.nodeName === 'PRE' && !hasSiblings; + + return node.nodeName === 'CODE' && !isCodeBlock; + }, + + replacement: function (inputContent: any) { + if (!inputContent) return ''; + let content = inputContent; + + let delimiter = '`'; + let matches = content.match(/`+/gm) || []; + while (matches.indexOf(delimiter) !== -1) delimiter = delimiter + '`'; + if (content.includes('\n')) { + delimiter = '```'; + } + + let extraSpace = delimiter === '```' ? '\n' : /^`|^ .*?[^ ].* $|`$/.test(content) ? ' ' : ''; + + return delimiter + extraSpace + content + (delimiter === '```' && !content.endsWith(extraSpace) ? extraSpace : '') + delimiter; + } + }); return turnDownService; } diff --git a/thinapps-shared b/thinapps-shared index a6116b7..5939c70 160000 --- a/thinapps-shared +++ b/thinapps-shared @@ -1 +1 @@ -Subproject commit a6116b73e99e3d335b0cd4cfcae8f4f0c7e72f6d +Subproject commit 5939c7091985706bebe7d1d83591430426b292c8