UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

60 lines (59 loc) 2.34 kB
import { chunksToText, walkRecursiveSync } from '../markdownNodesUtils.js'; import { applyRewriteRule } from '../applyRewriteRule.js'; // Related tests: // test ./rewrite-rules.md.markdown export function applyRewriteRules(markdownChunks, rewriteRule = []) { let inHtml = 0; walkRecursiveSync(markdownChunks.body, (chunk, ctx) => { if (chunk.isTag && chunk.tag === 'HTML_MODE/') { inHtml++; return; } if ('tag' in chunk && ['SVG/', 'IMG/'].includes(chunk.tag)) { for (const rule of rewriteRule) { const { shouldBreak, text } = applyRewriteRule(rule, { ...chunk, mode: inHtml ? 'html' : 'md', href: 'payload' in chunk ? chunk.payload?.href : undefined, alt: 'payload' in chunk ? chunk.payload?.alt : undefined }); if (shouldBreak) { const textNode = { isTag: false, text: text, parent: undefined, comment: 'MarkdownNodes.ts: appendText' }; chunk.parent.children.splice(ctx.nodeIdx, 1, textNode); break; } } } if ('tag' in chunk && 'A' === chunk.tag) { const alt = chunksToText(chunk.children, { ...ctx, mode: 'md', onlyNotTag: true }); for (const rule of rewriteRule) { const { shouldBreak, text } = applyRewriteRule(rule, { ...chunk, mode: inHtml ? 'html' : 'md', href: 'payload' in chunk ? chunk.payload?.href : undefined, alt }); if (shouldBreak) { const textNode = { isTag: false, text, parent: undefined, comment: 'MarkdownNodes.ts: appendText' }; chunk.parent.children.splice(ctx.nodeIdx, 1, textNode); break; } } } }, {}, (chunk) => { if (chunk.isTag && chunk.tag === 'HTML_MODE/') { inHtml--; return; } }); }