UNPKG

@mieweb/wikigdrive

Version:

Google Drive to MarkDown synchronization

33 lines (32 loc) 1.07 kB
import { extractText, walkRecursiveSync } from '../markdownNodesUtils.js'; // Related tests: // test ./header-link // test ./project-overview.md // test ./list-indent.md // test ./strong-headers.md export function fixIdLinks(markdownChunks) { let inHtml = false; walkRecursiveSync(markdownChunks.body, (chunk) => { if (chunk.isTag && chunk.tag === 'HTML_MODE/') { inHtml = true; return; } if (inHtml) { return; } if (chunk.isTag && 'A' === chunk.tag) { if (chunk.payload?.href && chunk.payload?.href.startsWith('#')) { const innerTxt = extractText(chunk); const escapedText = innerTxt.toLowerCase().replace(/[^\w]+/g, ' ').trim().replaceAll(' ', '-'); if (escapedText) { chunk.payload.href = '#' + escapedText; } } } }, {}, (chunk) => { if (chunk.isTag && chunk.tag === 'HTML_MODE/') { inHtml = false; return; } }); }