From 02c3492b3d574812b5391979e04c399e350a26ed Mon Sep 17 00:00:00 2001
From: 安竹洋平 <61961825+yasutakeyohei@users.noreply.github.com>
Date: Thu, 25 Jan 2024 00:15:16 +0900
Subject: initial commit
---
.../admonition-title-to-heading-before-toc.js | 49 +++++++++++
src/rehype/admonition-title-to-heading.js | 31 +++++++
.../admonition-title-to-heading-before-toc.js | 60 +++++++++++++
src/theme/Admonition.js | 6 ++
src/theme/Admonition/Layout/index.js | 99 ++++++++++++++++++++++
src/theme/Admonition/Type/Caution.js | 34 ++++++++
src/theme/Admonition/Type/Danger.js | 32 +++++++
src/theme/Admonition/Type/Info.js | 32 +++++++
src/theme/Admonition/Type/Note.js | 32 +++++++
src/theme/Admonition/Type/Tip.js | 32 +++++++
src/theme/Admonition/Type/Warning.js | 32 +++++++
src/theme/Admonition/Types.js | 33 ++++++++
src/theme/Admonition/index.js | 24 ++++++
13 files changed, 496 insertions(+)
create mode 100644 src/rehype/admonition-title-to-heading-before-toc.js
create mode 100644 src/rehype/admonition-title-to-heading.js
create mode 100644 src/remark/admonition-title-to-heading-before-toc.js
create mode 100644 src/theme/Admonition.js
create mode 100644 src/theme/Admonition/Layout/index.js
create mode 100644 src/theme/Admonition/Type/Caution.js
create mode 100644 src/theme/Admonition/Type/Danger.js
create mode 100644 src/theme/Admonition/Type/Info.js
create mode 100644 src/theme/Admonition/Type/Note.js
create mode 100644 src/theme/Admonition/Type/Tip.js
create mode 100644 src/theme/Admonition/Type/Warning.js
create mode 100644 src/theme/Admonition/Types.js
create mode 100644 src/theme/Admonition/index.js
(limited to 'src')
diff --git a/src/rehype/admonition-title-to-heading-before-toc.js b/src/rehype/admonition-title-to-heading-before-toc.js
new file mode 100644
index 00000000..2770610d
--- /dev/null
+++ b/src/rehype/admonition-title-to-heading-before-toc.js
@@ -0,0 +1,49 @@
+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
new file mode 100644
index 00000000..d84b27b2
--- /dev/null
+++ b/src/rehype/admonition-title-to-heading.js
@@ -0,0 +1,31 @@
+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
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
diff --git a/src/theme/Admonition.js b/src/theme/Admonition.js
new file mode 100644
index 00000000..cd35ae9d
--- /dev/null
+++ b/src/theme/Admonition.js
@@ -0,0 +1,6 @@
+import React from 'react';
+import Admonition from './Admonition';
+
+export default function AdmonitionWrapper(props) {
+ return