aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com>2024-01-27 01:22:37 +0900
committer安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com>2024-01-27 01:22:37 +0900
commitcc128057af3c0805f0c5d26e6a915690d37e2187 (patch)
tree399eff1173bb015b5e0fcb1e970ef7e6dfa04fd8 /src
parentb558db3d98102959c15ef662d9796e3f62557461 (diff)
blogに対応
Diffstat (limited to 'src')
-rw-r--r--src/rehype/admonition-title-to-heading-after-toc.js32
1 files changed, 17 insertions, 15 deletions
diff --git a/src/rehype/admonition-title-to-heading-after-toc.js b/src/rehype/admonition-title-to-heading-after-toc.js
index 5355983d..0b6771f1 100644
--- a/src/rehype/admonition-title-to-heading-after-toc.js
+++ b/src/rehype/admonition-title-to-heading-after-toc.js
@@ -14,22 +14,24 @@ const plugin = (options) => {
hContent = node.children ? node.children[0].value :
node.children[0].children[0] ? node.children[0].children[0].value : '';
- const nextNode = parent.children[index + 1];
- if (nextNode && nextNode.tagName === 'admonition') {
- // H要素に続くadmonition(div)を見つけた場合
+ // 続くAdmonitionを探す(docはH要素とadmonitionが連続しているが
+ // blogではなぜか改行要素{ type:'text', value:'\n' }が間に入っているので念のため隣接3要素を探す
+ for (let i = index + 1; i < index + 4 && i < parent.children.length; i++) {
+ if(parent.children[i] && parent.children[i].tagName === 'admonition') {
+ // admonition(div)を見つけた場合
+ const admonitionNode = parent.children[i];
+
+ // admonitionタイトルの冒頭Text部分を取得(properties.titleもしくはchildren[0].children[0].value)
+ const admonitionNodeTitle = admonitionNode.properties.title ? admonitionNode.properties.title :
+ admonitionNode.children[0] && admonitionNode.children[0].children[0] ? admonitionNode.children[0].children[0].value : '';
- // admonitionタイトルの冒頭Text部分を取得(properties.titleもしくはchildren[0].children[0].value)
- const nextNodeTitle = nextNode.properties.title ? nextNode.properties.title :
- nextNode.children[0] && nextNode.children[0].children[0] ? nextNode.children[0].children[0].value : '';
-
- //console.log(nextNodeTitle.replace(/^#+/, '').trim(), hId);
- if(/^##/.test(nextNodeTitle) && nextNodeTitle.replace(/^#+/, '').trim() === hContent.trim()) {
- // #で始まっていて、タイトル冒頭部が同じ場合
- // divのidをHタグのidに設定
- nextNode.properties.id = hId;
- //console.log(nextNode);
- // H要素を削除
- parent.children.splice(index, 1);
+ if(/^##/.test(admonitionNodeTitle) && admonitionNodeTitle.replace(/^#+/, '').trim() === hContent.trim()) {
+ // #で始まっていて、タイトル冒頭部が同じ場合
+ // divのidをHタグのidに設定
+ admonitionNode.properties.id = hId;
+ // H要素を削除
+ parent.children.splice(index, 1);
+ }
}
}
}