pack-test-yanmengs 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/components.ts +0 -1
  2. package/package.json +3 -1
  3. package/utils.ts +28 -37
package/components.ts CHANGED
@@ -126,7 +126,6 @@ customElements.define(
126
126
  }
127
127
  },
128
128
  );
129
-
130
129
  customElements.define(
131
130
  "doc-card",
132
131
  class extends HTMLElement {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pack-test-yanmengs",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "",
5
5
  "private": false,
6
6
  "main": "index.js",
@@ -15,8 +15,10 @@
15
15
  "dependencies": {
16
16
  "mdast": "^3.0.0",
17
17
  "mdast-util-directive": "^3.1.0",
18
+ "rehype-stringify": "^10.0.1",
18
19
  "remark-directive": "^4.0.0",
19
20
  "remark-parse": "^11.0.0",
21
+ "remark-rehype": "^11.1.2",
20
22
  "typescript": "^5.9.3",
21
23
  "unified": "^11.0.5"
22
24
  },
package/utils.ts CHANGED
@@ -1,15 +1,15 @@
1
1
  import { unified } from "unified";
2
2
  import remarkParse from "remark-parse";
3
3
  import remarkDirective from "remark-directive";
4
- import type { Root, Content, Text, Parent, Code } from "mdast";
4
+ import type { Root, Content, Text, Parent } from "mdast";
5
5
  import type { ContainerDirective } from "mdast-util-directive";
6
-
6
+ import remarkRehype from "remark-rehype";
7
+ import rehypeStringify from "rehype-stringify";
7
8
  /**
8
9
  * 获取 Markdown AST (抽象语法树)
9
10
  */
10
11
  function getAST(content: string): Root {
11
12
  const processor = unified().use(remarkParse).use(remarkDirective);
12
- console.log(processor.parse(content), "processor.parse(content)", content);
13
13
  return processor.parse(content) as Root;
14
14
  }
15
15
 
@@ -44,9 +44,8 @@ const parseAST = (
44
44
  ? `</doc-${directiveNode.name}>`
45
45
  : `<doc-${directiveNode.name}${attrString ? " " + attrString : ""}>`;
46
46
 
47
- // 3. 创建替换文本节点
48
- const textNode: Text = {
49
- type: "text",
47
+ const htmlNode: any = {
48
+ type: "html",
50
49
  value: tagName,
51
50
  };
52
51
 
@@ -57,7 +56,7 @@ const parseAST = (
57
56
  map.set(column, true);
58
57
  }
59
58
 
60
- newChildren.push(textNode);
59
+ newChildren.push(htmlNode);
61
60
 
62
61
  // 5. 递归处理子节点并将结果展开(平铺化)
63
62
  if (directiveNode.children) {
@@ -68,6 +67,7 @@ const parseAST = (
68
67
  const processedInner = parseAST(getAST(String(node.value)));
69
68
  newChildren.push(...processedInner);
70
69
  } else {
70
+ console.log(node, "node");
71
71
  // 普通节点递归处理(如果存在子节点)
72
72
  if ("children" in node) {
73
73
  node.children = parseAST(node as Parent, map) as any;
@@ -79,37 +79,28 @@ const parseAST = (
79
79
  return newChildren;
80
80
  };
81
81
 
82
- /**
83
- * 将 AST 节点拍平并提取文本内容 (HTML 字符串)
84
- */
85
- const parseHtml = (nodes: Content[], currentStr: string = ""): string => {
86
- let result = currentStr;
87
-
88
- nodes.forEach((node) => {
89
- if ("children" in node && node.children) {
90
- result = parseHtml(node.children as Content[], result);
91
- } else if ("value" in node) {
92
- result += node.value + "\n";
93
- }
94
- });
95
-
96
- return result;
97
- };
98
- const MarkdownToHtml = (markdownContent: string): string => {
82
+ export const transformMarkdownToHtml = async (
83
+ markdownContent: string,
84
+ ): Promise<string> => {
85
+ // 1. 依然使用你原来的逻辑生成转换后的 AST 节点数组
99
86
  const ast = getAST(markdownContent);
100
87
  const transformedChildren = parseAST(ast);
101
- return parseHtml(transformedChildren);
102
- };
103
- /**
104
- * 主导出函数
105
- */
106
- export const transformMarkdownToHtml = (markdownContent: string): string => {
107
- let result = MarkdownToHtml(markdownContent);
108
- // while (result.includes(":::")) {
109
- // result = MarkdownToHtml(result);
110
- // console.log(result);
111
- // }
112
88
 
113
- console.log(result);
114
- return result;
89
+ // 2. 构造一个新的 Root 节点
90
+ const transformedAst: Root = {
91
+ type: "root",
92
+ children: transformedChildren as any,
93
+ };
94
+
95
+ // 3. 【核心步骤】交给 unified 处理原本的 md 语法
96
+ const processor = unified()
97
+ .use(remarkRehype, { allowDangerousHtml: true }) // 允许你 push 的 html 节点通过
98
+ .use(rehypeStringify, { allowDangerousHtml: true });
99
+
100
+ // 运行转换:这一步会将树里的 Heading 变成 h1, List 变成 ul/li
101
+ const hastTree = await processor.run(transformedAst);
102
+
103
+ // 将处理完的树序列化为 HTML 字符串
104
+ const finalHtml = processor.stringify(hastTree);
105
+ return String(finalHtml);
115
106
  };