aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
author安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com>2024-01-25 21:25:17 +0900
committer安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com>2024-01-25 21:25:17 +0900
commitf1a44937cdbdbc4d4fb4acc0088be38b18260ffe (patch)
tree98893788fff37aa4829c4427930b3c693f830d57 /src
parentdbecbf22c1f18c32c7b057ed8efe7ada5e2a65de (diff)
ファイル名変更
Diffstat (limited to 'src')
-rw-r--r--src/rehype/admonition-title-to-heading-after-toc.js41
-rw-r--r--src/rehype/admonition-title-to-heading-before-toc.js49
-rw-r--r--src/rehype/admonition-title-to-heading.js31
3 files changed, 41 insertions, 80 deletions
diff --git a/src/rehype/admonition-title-to-heading-after-toc.js b/src/rehype/admonition-title-to-heading-after-toc.js
new file mode 100644
index 00000000..5355983d
--- /dev/null
+++ b/src/rehype/admonition-title-to-heading-after-toc.js
@@ -0,0 +1,41 @@
+import {visit} from 'unist-util-visit';
+import {inspect} from 'unist-util-inspect';
+
+const plugin = (options) => {
+ const transformer = async (ast) => {
+ let hId = null;
+ let hContent = null;
+ visit(ast, 'element', (node, index, parent) => {
+ if (/^h[2-6]$/.test(node.tagName) && node.properties && node.properties.id) {
+ // H要素(h2~h6)を見つけた場合
+
+ // IDとタイトルの冒頭Text部を取得する
+ hId = node.properties.id;
+ 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タイトルの冒頭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);
+ }
+ }
+ }
+ });
+ };
+ return transformer;
+};
+
+export default plugin; \ No newline at end of file
diff --git a/src/rehype/admonition-title-to-heading-before-toc.js b/src/rehype/admonition-title-to-heading-before-toc.js
deleted file mode 100644
index 2770610d..00000000
--- a/src/rehype/admonition-title-to-heading-before-toc.js
+++ /dev/null
@@ -1,49 +0,0 @@
-import {visit} from 'unist-util-visit';
-import {u} from 'unist-builder'
-
-function createHeadingNode(depth, text) {
- return {
- type: 'heading',
- depth: depth,
- children: [{ type: 'text', value: text }],
- };
-}
-
-const plugin = (options) => {
- const transformer = async (ast) => {
- let hId = null;
- let hContent = null;
- let offset = 0;
- visit(ast, 'element', (node, index, parent) => {
- if(node && node.tagName == "admonition") {
- let headingNode = u('heading', { depth: 4 }, [
- u('text', 'New Heading')
- ]);
- parent.children.splice(index + offset, 0, {type: 'element', tagName: 'h4', children:[{type: 'text', value: 'テスト'}]});
- offset++;
- // 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
diff --git a/src/rehype/admonition-title-to-heading.js b/src/rehype/admonition-title-to-heading.js
deleted file mode 100644
index d84b27b2..00000000
--- a/src/rehype/admonition-title-to-heading.js
+++ /dev/null
@@ -1,31 +0,0 @@
-import {visit} from 'unist-util-visit';
-
-const plugin = (options) => {
- const transformer = async (ast) => {
- let hId = null;
- let hContent = null;
- visit(ast, 'element', (node, index, parent) => {
- if (/^h[2-6]$/.test(node.tagName) && node.properties && node.properties.id) {
- // h2~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