diff --git a/md-viewer.html b/md-viewer.html index 762829a..d3584ea 100644 --- a/md-viewer.html +++ b/md-viewer.html @@ -78,25 +78,59 @@ if (filePath) { /* 检测是否为自带 HTML 结构的文件(如共性需求说明.md) */ const isRawHtml = /^\s*<(?:style|div|!doctype|html)/i.test(md); if (isRawHtml) { - /* 文件混合了 HTML 结构和 Markdown 内容,需先解析 Markdown 再渲染 */ + /* 为标题生成锚点 id(marked 新版不再自动生成) */ + function addHeadingIds(html) { + return html.replace(/([\s\S]*?)<\/h\1>/g, function(_, lvl, inner) { + var plain = inner.replace(/<[^>]*>/g, ''); + var id = plain.toLowerCase().replace(/[^\p{L}\p{N}\s-]/gu, '').trim().replace(/\s+/g, '-'); + return '' + inner + ''; + }); + } let processed = md; if (typeof marked !== 'undefined') { - /* 保留 '; + /* iframe 内注入:滚动时高亮当前目录项 */ + const tocScript = '