diff options
author | 安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com> | 2024-01-25 00:15:16 +0900 |
---|---|---|
committer | 安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com> | 2024-01-25 00:15:16 +0900 |
commit | 02c3492b3d574812b5391979e04c399e350a26ed (patch) | |
tree | 2b492ac9564da6b201d035eb4434ea76e3f64478 /src/remark/admonition-title-to-heading-before-toc.js | |
parent | 5970320e4994df7652eedea9e16770e1720342f8 (diff) |
initial commit
Diffstat (limited to 'src/remark/admonition-title-to-heading-before-toc.js')
-rw-r--r-- | src/remark/admonition-title-to-heading-before-toc.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/remark/admonition-title-to-heading-before-toc.js b/src/remark/admonition-title-to-heading-before-toc.js new file mode 100644 index 00000000..34d2f9fa --- /dev/null +++ b/src/remark/admonition-title-to-heading-before-toc.js @@ -0,0 +1,60 @@ +import {visit, SKIP, EXIT, INDEX, CONTINUE} from 'unist-util-visit'; +import {inspect} from 'unist-util-inspect' + +const plugin = (options) => { + const transformer = async (ast) => { + let hId = null; + let hContent = null; + let offset = 0; + + // Visitor関数を定義 + const visitor = ((node, index, parent) => { + if (node.type === 'containerDirective') { + const val = node.children[0].children[0].value; + if(/^##/.test(val)) { + // headingノードをparentのchildrenに追加 + parent.children.splice(index, 0, { + type: 'heading', + depth: 4, // Headingの深さを適切に設定 + children: [{ type: 'text', value: val.replace(/^#+/, '').trim() }], + }); + //次に検索するのはindexを2つ飛ばしたノード。 + return index + 2; + } + } + }); + + visit(ast, 'containerDirective', visitor); + /* + visit(ast, 'containerDirective', (node, index, parent) => { + //console.log(node); + parent.children.splice(index, 0, {type: 'heading', depth: 4, children:[{type: 'text', value: 'テスト'}]}); + index++; + //offset++; + //console.log(offset); + //return EXIT; + // if (/^h[3-6]$/.test(node.tagName) && node.properties && node.properties.id) { + // h3~h6のタグを見つけたら + /* + hId = node.properties.id; + hContent = node.children && node.children[0] ? node.children[0].value : ''; + // h3~h6タグの隣にあるdivタグを探す + const nextNode = parent.children[index + 1]; + if (nextNode && nextNode.tagName === 'admonition') { + // 該当のdiv要素を見つけたらHタグの内容とline-5の#以降の文字列が一致した場合 + const contentAfterHash = nextNode.properties.title ? nextNode.properties.title.replace(/^#+/, '').trim() : ''; + + if (contentAfterHash === hContent.trim()) { + // Hタグのidを取得しそれをdivのidに設定 + nextNode.properties.id = hId; + // Hタグを削除 + parent.children.splice(index, 1); + } + }*/ +// } +// }); + }; + return transformer; +}; + +export default plugin;
\ No newline at end of file |