|
@@ -9,6 +9,35 @@ import { visit } from 'unist-util-visit';
|
|
|
|
|
|
|
|
const SUPPORTED_ATTRIBUTES = ['children', 'marp'];
|
|
const SUPPORTED_ATTRIBUTES = ['children', 'marp'];
|
|
|
|
|
|
|
|
|
|
+const nodeToMakrdown = (node: Node) => {
|
|
|
|
|
+ return toMarkdown(node as Root, {
|
|
|
|
|
+ extensions: [
|
|
|
|
|
+ frontmatterToMarkdown(['yaml']),
|
|
|
|
|
+ gfmToMarkdown(),
|
|
|
|
|
+ ],
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+// Allow node tree to be converted to markdown
|
|
|
|
|
+const removeCustomType = (tree: Node) => {
|
|
|
|
|
+ // Try toMarkdown() on all Node.
|
|
|
|
|
+ visit(tree, (node) => {
|
|
|
|
|
+ const tmp = node?.children;
|
|
|
|
|
+ node.children = [];
|
|
|
|
|
+ try {
|
|
|
|
|
+ nodeToMakrdown(node);
|
|
|
|
|
+ }
|
|
|
|
|
+ catch (err) {
|
|
|
|
|
+ // if some Node cannot convert to markdown, change to a convertible type
|
|
|
|
|
+ node.type = 'text';
|
|
|
|
|
+ node.value = '';
|
|
|
|
|
+ }
|
|
|
|
|
+ finally {
|
|
|
|
|
+ node.children = tmp;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
const rewriteNode = (tree: Node, node: Node, isEnabledMarp: boolean) => {
|
|
const rewriteNode = (tree: Node, node: Node, isEnabledMarp: boolean) => {
|
|
|
let slide = false;
|
|
let slide = false;
|
|
|
let marp = false;
|
|
let marp = false;
|
|
@@ -32,33 +61,9 @@ const rewriteNode = (tree: Node, node: Node, isEnabledMarp: boolean) => {
|
|
|
|
|
|
|
|
if (marp || slide) {
|
|
if (marp || slide) {
|
|
|
|
|
|
|
|
- visit(tree, (node) => {
|
|
|
|
|
- const tmp = node?.children;
|
|
|
|
|
- node.children = [];
|
|
|
|
|
- try {
|
|
|
|
|
- toMarkdown(node as Root, {
|
|
|
|
|
- extensions: [
|
|
|
|
|
- frontmatterToMarkdown(['yaml']),
|
|
|
|
|
- gfmToMarkdown(),
|
|
|
|
|
- ],
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- catch (err) {
|
|
|
|
|
- node.type = 'text';
|
|
|
|
|
- node.value = '';
|
|
|
|
|
- }
|
|
|
|
|
- finally {
|
|
|
|
|
- node.children = tmp;
|
|
|
|
|
- }
|
|
|
|
|
- });
|
|
|
|
|
-
|
|
|
|
|
- const markdown = toMarkdown(tree as Root, {
|
|
|
|
|
- extensions: [
|
|
|
|
|
- frontmatterToMarkdown(['yaml']),
|
|
|
|
|
- gfmToMarkdown(),
|
|
|
|
|
- ],
|
|
|
|
|
- });
|
|
|
|
|
|
|
+ removeCustomType(tree);
|
|
|
|
|
|
|
|
|
|
+ const markdown = nodeToMakrdown(tree);
|
|
|
|
|
|
|
|
const newNode: Node = {
|
|
const newNode: Node = {
|
|
|
type: 'root',
|
|
type: 'root',
|
|
@@ -67,7 +72,6 @@ const rewriteNode = (tree: Node, node: Node, isEnabledMarp: boolean) => {
|
|
|
children: tree.children,
|
|
children: tree.children,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-
|
|
|
|
|
const data = newNode.data ?? (newNode.data = {});
|
|
const data = newNode.data ?? (newNode.data = {});
|
|
|
tree.children = [newNode];
|
|
tree.children = [newNode];
|
|
|
data.hName = 'slide';
|
|
data.hName = 'slide';
|