pd-markdown 1.0.0 → 1.1.0

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 (170) hide show
  1. package/README.md +293 -0
  2. package/package.json +69 -1
  3. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/index.d.ts +0 -4
  4. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/index.d.ts.map +0 -1
  5. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/index.js +0 -5
  6. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/index.js.map +0 -1
  7. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/index.d.ts +0 -4
  8. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/index.d.ts.map +0 -1
  9. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/index.js +0 -4
  10. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/index.js.map +0 -1
  11. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/heading.d.ts +0 -6
  12. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/heading.d.ts.map +0 -1
  13. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/heading.js +0 -36
  14. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/heading.js.map +0 -1
  15. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/list.d.ts +0 -14
  16. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/list.d.ts.map +0 -1
  17. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/list.js +0 -18
  18. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/list.js.map +0 -1
  19. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/table.d.ts +0 -27
  20. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/table.d.ts.map +0 -1
  21. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/table.js +0 -37
  22. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/plugins/transform/table.js.map +0 -1
  23. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/processor.d.ts +0 -22
  24. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/processor.d.ts.map +0 -1
  25. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/processor.js +0 -95
  26. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/processor.js.map +0 -1
  27. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/types/index.d.ts +0 -55
  28. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/types/index.d.ts.map +0 -1
  29. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/types/index.js +0 -2
  30. package/packages/parser/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/parser/dist/types/index.js.map +0 -1
  31. package/packages/parser/__tests__/frontmatter.test.ts +0 -69
  32. package/packages/parser/__tests__/gfm.test.ts +0 -83
  33. package/packages/parser/__tests__/processor.test.ts +0 -136
  34. package/packages/parser/__tests__/transform/heading.test.ts +0 -56
  35. package/packages/parser/__tests__/transform/list.test.ts +0 -67
  36. package/packages/parser/__tests__/transform/table.test.ts +0 -85
  37. package/packages/parser/node_modules/.bin/yaml +0 -17
  38. package/packages/parser/package.json +0 -38
  39. package/packages/parser/rollup.config.ts +0 -38
  40. package/packages/parser/src/index.ts +0 -15
  41. package/packages/parser/src/plugins/index.ts +0 -3
  42. package/packages/parser/src/plugins/transform/heading.ts +0 -40
  43. package/packages/parser/src/plugins/transform/list.ts +0 -29
  44. package/packages/parser/src/plugins/transform/table.ts +0 -62
  45. package/packages/parser/src/processor.ts +0 -119
  46. package/packages/parser/src/types/index.ts +0 -60
  47. package/packages/parser/tsconfig.json +0 -9
  48. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/query.d.ts +0 -36
  49. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/query.d.ts.map +0 -1
  50. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/query.js +0 -99
  51. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/query.js.map +0 -1
  52. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/traverse.d.ts +0 -22
  53. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/traverse.d.ts.map +0 -1
  54. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/traverse.js +0 -46
  55. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/ast/traverse.js.map +0 -1
  56. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/index.d.ts +0 -7
  57. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/index.d.ts.map +0 -1
  58. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/index.js +0 -8
  59. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/index.js.map +0 -1
  60. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/sanitize.d.ts +0 -22
  61. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/sanitize.d.ts.map +0 -1
  62. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/sanitize.js +0 -140
  63. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/sanitize.js.map +0 -1
  64. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/slugify.d.ts +0 -16
  65. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/slugify.d.ts.map +0 -1
  66. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/slugify.js +0 -39
  67. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/string/slugify.js.map +0 -1
  68. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/types/index.d.ts +0 -49
  69. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/types/index.d.ts.map +0 -1
  70. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/types/index.js +0 -19
  71. package/packages/utils/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/utils/dist/types/index.js.map +0 -1
  72. package/packages/utils/__tests__/query.test.ts +0 -155
  73. package/packages/utils/__tests__/sanitize.test.ts +0 -96
  74. package/packages/utils/__tests__/slugify.test.ts +0 -71
  75. package/packages/utils/__tests__/traverse.test.ts +0 -131
  76. package/packages/utils/package.json +0 -27
  77. package/packages/utils/rollup.config.ts +0 -26
  78. package/packages/utils/src/ast/query.ts +0 -127
  79. package/packages/utils/src/ast/traverse.ts +0 -73
  80. package/packages/utils/src/index.ts +0 -20
  81. package/packages/utils/src/string/sanitize.ts +0 -155
  82. package/packages/utils/src/string/slugify.ts +0 -43
  83. package/packages/utils/src/types/index.ts +0 -72
  84. package/packages/utils/tsconfig.json +0 -8
  85. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/MarkdownRenderer.d.ts +0 -27
  86. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/MarkdownRenderer.d.ts.map +0 -1
  87. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/MarkdownRenderer.js +0 -39
  88. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/MarkdownRenderer.js.map +0 -1
  89. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/NodeRenderer.d.ts +0 -10
  90. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/NodeRenderer.d.ts.map +0 -1
  91. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/NodeRenderer.js +0 -130
  92. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/NodeRenderer.js.map +0 -1
  93. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/context.d.ts +0 -17
  94. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/context.d.ts.map +0 -1
  95. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/context.js +0 -14
  96. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/context.js.map +0 -1
  97. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Blockquote.d.ts +0 -8
  98. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Blockquote.d.ts.map +0 -1
  99. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Blockquote.js +0 -5
  100. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Blockquote.js.map +0 -1
  101. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Code.d.ts +0 -13
  102. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Code.d.ts.map +0 -1
  103. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Code.js +0 -9
  104. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Code.js.map +0 -1
  105. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Heading.d.ts +0 -8
  106. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Heading.d.ts.map +0 -1
  107. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Heading.js +0 -7
  108. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Heading.js.map +0 -1
  109. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Image.d.ts +0 -7
  110. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Image.d.ts.map +0 -1
  111. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Image.js +0 -5
  112. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Image.js.map +0 -1
  113. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Link.d.ts +0 -8
  114. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Link.d.ts.map +0 -1
  115. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Link.js +0 -7
  116. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Link.js.map +0 -1
  117. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/List.d.ts +0 -13
  118. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/List.d.ts.map +0 -1
  119. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/List.js +0 -14
  120. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/List.js.map +0 -1
  121. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Paragraph.d.ts +0 -8
  122. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Paragraph.d.ts.map +0 -1
  123. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Paragraph.js +0 -5
  124. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Paragraph.js.map +0 -1
  125. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Table.d.ts +0 -19
  126. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Table.d.ts.map +0 -1
  127. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Table.js +0 -18
  128. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/Table.js.map +0 -1
  129. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/index.d.ts +0 -34
  130. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/index.d.ts.map +0 -1
  131. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/index.js +0 -28
  132. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/components/defaults/index.js.map +0 -1
  133. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/hooks/useMarkdown.d.ts +0 -11
  134. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/hooks/useMarkdown.d.ts.map +0 -1
  135. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/hooks/useMarkdown.js +0 -28
  136. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/hooks/useMarkdown.js.map +0 -1
  137. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/index.d.ts +0 -6
  138. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/index.d.ts.map +0 -1
  139. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/index.js +0 -9
  140. package/packages/web/.rollup.cache/Users/pidan/Work/Learn/pd-markdown/packages/web/dist/index.js.map +0 -1
  141. package/packages/web/__tests__/MarkdownRenderer.test.tsx +0 -89
  142. package/packages/web/__tests__/NodeRenderer.test.tsx +0 -97
  143. package/packages/web/__tests__/components/Code.test.tsx +0 -71
  144. package/packages/web/__tests__/components/Heading.test.tsx +0 -65
  145. package/packages/web/__tests__/components/List.test.tsx +0 -100
  146. package/packages/web/__tests__/components/Table.test.tsx +0 -105
  147. package/packages/web/__tests__/useMarkdown.test.ts +0 -63
  148. package/packages/web/package.json +0 -40
  149. package/packages/web/rollup.config.ts +0 -36
  150. package/packages/web/src/components/MarkdownRenderer.tsx +0 -70
  151. package/packages/web/src/components/NodeRenderer.tsx +0 -205
  152. package/packages/web/src/components/context.ts +0 -24
  153. package/packages/web/src/components/defaults/Blockquote.tsx +0 -11
  154. package/packages/web/src/components/defaults/Code.tsx +0 -26
  155. package/packages/web/src/components/defaults/Heading.tsx +0 -14
  156. package/packages/web/src/components/defaults/Image.tsx +0 -10
  157. package/packages/web/src/components/defaults/Link.tsx +0 -18
  158. package/packages/web/src/components/defaults/List.tsx +0 -33
  159. package/packages/web/src/components/defaults/Paragraph.tsx +0 -11
  160. package/packages/web/src/components/defaults/Table.tsx +0 -50
  161. package/packages/web/src/components/defaults/index.tsx +0 -80
  162. package/packages/web/src/hooks/useMarkdown.ts +0 -32
  163. package/packages/web/src/index.ts +0 -37
  164. package/packages/web/tsconfig.json +0 -11
  165. package/packages/web/vitest.config.ts +0 -9
  166. package/pnpm-workspace.yaml +0 -2
  167. package/tsconfig.base.json +0 -26
  168. package/tsconfig.json +0 -8
  169. package/vitest.config.ts +0 -28
  170. package/vitest.setup.ts +0 -1
@@ -1,131 +0,0 @@
1
- import { describe, it, expect, vi } from 'vitest'
2
- import { traverseAst, traverseAstWithCallbacks } from '../src/ast/traverse'
3
- import type { Root, Paragraph, Text, Heading } from 'mdast'
4
-
5
- describe('traverseAst', () => {
6
- const createSimpleAst = (): Root => ({
7
- type: 'root',
8
- children: [
9
- {
10
- type: 'heading',
11
- depth: 1,
12
- children: [{ type: 'text', value: 'Title' }],
13
- } as Heading,
14
- {
15
- type: 'paragraph',
16
- children: [{ type: 'text', value: 'Content' }],
17
- } as Paragraph,
18
- ],
19
- })
20
-
21
- it('should visit all nodes in depth-first order', () => {
22
- const ast = createSimpleAst()
23
- const visited: string[] = []
24
-
25
- traverseAst(ast, (node) => {
26
- visited.push(node.type)
27
- })
28
-
29
- expect(visited).toEqual(['root', 'heading', 'text', 'paragraph', 'text'])
30
- })
31
-
32
- it('should skip children when visitor returns false', () => {
33
- const ast = createSimpleAst()
34
- const visited: string[] = []
35
-
36
- traverseAst(ast, (node) => {
37
- visited.push(node.type)
38
- if (node.type === 'heading') {
39
- return false // Skip children of heading
40
- }
41
- })
42
-
43
- expect(visited).toEqual(['root', 'heading', 'paragraph', 'text'])
44
- })
45
-
46
- it('should provide correct index and parent', () => {
47
- const ast = createSimpleAst()
48
- const visitor = vi.fn()
49
-
50
- traverseAst(ast, visitor)
51
-
52
- // Root has no index and no parent
53
- expect(visitor).toHaveBeenNthCalledWith(1, ast, undefined, undefined)
54
-
55
- // Heading is at index 0 with root as parent
56
- expect(visitor).toHaveBeenNthCalledWith(2, ast.children[0], 0, ast)
57
- })
58
-
59
- it('should handle empty children', () => {
60
- const ast: Root = {
61
- type: 'root',
62
- children: [],
63
- }
64
- const visited: string[] = []
65
-
66
- traverseAst(ast, (node) => {
67
- visited.push(node.type)
68
- })
69
-
70
- expect(visited).toEqual(['root'])
71
- })
72
- })
73
-
74
- describe('traverseAstWithCallbacks', () => {
75
- const createSimpleAst = (): Root => ({
76
- type: 'root',
77
- children: [
78
- {
79
- type: 'paragraph',
80
- children: [{ type: 'text', value: 'Hello' }],
81
- } as Paragraph,
82
- ],
83
- })
84
-
85
- it('should call enter and leave in correct order', () => {
86
- const ast = createSimpleAst()
87
- const events: string[] = []
88
-
89
- traverseAstWithCallbacks(ast, {
90
- enter: (node) => {
91
- events.push(`enter:${node.type}`)
92
- },
93
- leave: (node) => {
94
- events.push(`leave:${node.type}`)
95
- },
96
- })
97
-
98
- expect(events).toEqual([
99
- 'enter:root',
100
- 'enter:paragraph',
101
- 'enter:text',
102
- 'leave:text',
103
- 'leave:paragraph',
104
- 'leave:root',
105
- ])
106
- })
107
-
108
- it('should skip children when enter returns false', () => {
109
- const ast = createSimpleAst()
110
- const events: string[] = []
111
-
112
- traverseAstWithCallbacks(ast, {
113
- enter: (node) => {
114
- events.push(`enter:${node.type}`)
115
- if (node.type === 'paragraph') {
116
- return false
117
- }
118
- },
119
- leave: (node) => {
120
- events.push(`leave:${node.type}`)
121
- },
122
- })
123
-
124
- expect(events).toEqual([
125
- 'enter:root',
126
- 'enter:paragraph',
127
- 'leave:paragraph',
128
- 'leave:root',
129
- ])
130
- })
131
- })
@@ -1,27 +0,0 @@
1
- {
2
- "name": "@pd-markdown/utils",
3
- "version": "0.1.0",
4
- "description": "Utility functions and types for pd-markdown",
5
- "type": "module",
6
- "exports": {
7
- ".": {
8
- "import": "./dist/index.mjs",
9
- "require": "./dist/index.cjs",
10
- "types": "./dist/index.d.ts"
11
- }
12
- },
13
- "main": "./dist/index.cjs",
14
- "module": "./dist/index.mjs",
15
- "types": "./dist/index.d.ts",
16
- "files": ["dist"],
17
- "scripts": {
18
- "build": "rollup -c rollup.config.ts --configPlugin typescript",
19
- "clean": "rm -rf dist *.tsbuildinfo"
20
- },
21
- "devDependencies": {
22
- "@types/mdast": "^4.0.4",
23
- "@types/unist": "^3.0.3"
24
- },
25
- "sideEffects": false,
26
- "license": "MIT"
27
- }
@@ -1,26 +0,0 @@
1
- import typescript from '@rollup/plugin-typescript'
2
- import { defineConfig } from 'rollup'
3
-
4
- export default defineConfig({
5
- input: 'src/index.ts',
6
- output: [
7
- {
8
- file: 'dist/index.mjs',
9
- format: 'esm',
10
- sourcemap: true,
11
- },
12
- {
13
- file: 'dist/index.cjs',
14
- format: 'cjs',
15
- sourcemap: true,
16
- },
17
- ],
18
- plugins: [
19
- typescript({
20
- tsconfig: './tsconfig.json',
21
- declaration: true,
22
- declarationDir: './dist',
23
- }),
24
- ],
25
- external: [],
26
- })
@@ -1,127 +0,0 @@
1
- import type { Node } from 'unist'
2
- import type { MdNode, Parent } from '../types'
3
- import { isParent } from '../types'
4
-
5
- /**
6
- * Find all nodes of a specific type in the AST
7
- *
8
- * @param node - Root node to search from
9
- * @param type - Node type to find
10
- * @returns Array of matching nodes
11
- */
12
- export function findNodes<T extends Node = MdNode>(
13
- node: Node,
14
- type: string
15
- ): T[] {
16
- const results: T[] = []
17
-
18
- function visit(current: Node): void {
19
- if (current.type === type) {
20
- results.push(current as T)
21
- }
22
-
23
- if (isParent(current)) {
24
- for (const child of current.children) {
25
- visit(child)
26
- }
27
- }
28
- }
29
-
30
- visit(node)
31
- return results
32
- }
33
-
34
- /**
35
- * Find the first node of a specific type in the AST
36
- *
37
- * @param node - Root node to search from
38
- * @param type - Node type to find
39
- * @returns The first matching node or undefined
40
- */
41
- export function findNode<T extends Node = MdNode>(
42
- node: Node,
43
- type: string
44
- ): T | undefined {
45
- let result: T | undefined
46
-
47
- function visit(current: Node): boolean {
48
- if (current.type === type) {
49
- result = current as T
50
- return true // Found, stop searching
51
- }
52
-
53
- if (isParent(current)) {
54
- for (const child of current.children) {
55
- if (visit(child)) {
56
- return true
57
- }
58
- }
59
- }
60
-
61
- return false
62
- }
63
-
64
- visit(node)
65
- return result
66
- }
67
-
68
- /**
69
- * Find all nodes matching a predicate
70
- *
71
- * @param node - Root node to search from
72
- * @param predicate - Function to test each node
73
- * @returns Array of matching nodes
74
- */
75
- export function findNodesBy<T extends Node = MdNode>(
76
- node: Node,
77
- predicate: (node: Node) => boolean
78
- ): T[] {
79
- const results: T[] = []
80
-
81
- function visit(current: Node): void {
82
- if (predicate(current)) {
83
- results.push(current as T)
84
- }
85
-
86
- if (isParent(current)) {
87
- for (const child of current.children) {
88
- visit(child)
89
- }
90
- }
91
- }
92
-
93
- visit(node)
94
- return results
95
- }
96
-
97
- /**
98
- * Get the parent of a node in the AST
99
- * Note: This requires traversing from root, use sparingly
100
- *
101
- * @param root - Root node of the AST
102
- * @param target - Node to find parent of
103
- * @returns Parent node or undefined if not found or is root
104
- */
105
- export function findParent(root: Node, target: Node): Parent | undefined {
106
- let result: Parent | undefined
107
-
108
- function visit(current: Node, parent: Parent | undefined): boolean {
109
- if (current === target) {
110
- result = parent
111
- return true
112
- }
113
-
114
- if (isParent(current)) {
115
- for (const child of current.children) {
116
- if (visit(child, current)) {
117
- return true
118
- }
119
- }
120
- }
121
-
122
- return false
123
- }
124
-
125
- visit(root, undefined)
126
- return result
127
- }
@@ -1,73 +0,0 @@
1
- import type { Node } from 'unist'
2
- import type { Parent, MdNode, Visitor } from '../types'
3
- import { isParent } from '../types'
4
-
5
- /**
6
- * Traverse AST in depth-first order
7
- *
8
- * @param node - Root node to start traversal
9
- * @param visitor - Visitor function called for each node
10
- * Return `false` to skip children of current node
11
- * Return `true` or `undefined` to continue
12
- */
13
- export function traverseAst<T extends Node = MdNode>(
14
- node: T,
15
- visitor: Visitor<T>
16
- ): void {
17
- function visit(
18
- current: T,
19
- index: number | undefined,
20
- parent: Parent | undefined
21
- ): void {
22
- const result = visitor(current, index, parent)
23
-
24
- // Skip children if visitor returns false
25
- if (result === false) {
26
- return
27
- }
28
-
29
- if (isParent(current)) {
30
- const children = current.children as T[]
31
- for (let i = 0; i < children.length; i++) {
32
- visit(children[i], i, current as unknown as Parent)
33
- }
34
- }
35
- }
36
-
37
- visit(node, undefined, undefined)
38
- }
39
-
40
- /**
41
- * Traverse AST with enter and leave callbacks
42
- *
43
- * @param node - Root node to start traversal
44
- * @param callbacks - Object with optional enter and leave functions
45
- */
46
- export function traverseAstWithCallbacks<T extends Node = MdNode>(
47
- node: T,
48
- callbacks: {
49
- enter?: Visitor<T>
50
- leave?: Visitor<T>
51
- }
52
- ): void {
53
- const { enter, leave } = callbacks
54
-
55
- function visit(
56
- current: T,
57
- index: number | undefined,
58
- parent: Parent | undefined
59
- ): void {
60
- const shouldSkipChildren = enter?.(current, index, parent) === false
61
-
62
- if (!shouldSkipChildren && isParent(current)) {
63
- const children = current.children as T[]
64
- for (let i = 0; i < children.length; i++) {
65
- visit(children[i], i, current as unknown as Parent)
66
- }
67
- }
68
-
69
- leave?.(current, index, parent)
70
- }
71
-
72
- visit(node, undefined, undefined)
73
- }
@@ -1,20 +0,0 @@
1
- // Types
2
- export type {
3
- MdNode,
4
- MdRoot,
5
- Parent,
6
- Literal,
7
- Visitor,
8
- PluginOptions,
9
- Position,
10
- Location,
11
- } from './types'
12
- export { isParent, isLiteral, isNodeType } from './types'
13
-
14
- // AST utilities
15
- export { traverseAst, traverseAstWithCallbacks } from './ast/traverse'
16
- export { findNodes, findNode, findNodesBy, findParent } from './ast/query'
17
-
18
- // String utilities
19
- export { slugify, uniqueSlugify } from './string/slugify'
20
- export { escapeHtml, sanitizeHtml, stripHtml } from './string/sanitize'
@@ -1,155 +0,0 @@
1
- /**
2
- * HTML entities that need escaping
3
- */
4
- const HTML_ESCAPE_MAP: Record<string, string> = {
5
- '&': '&amp;',
6
- '<': '&lt;',
7
- '>': '&gt;',
8
- '"': '&quot;',
9
- "'": '&#39;',
10
- }
11
-
12
- /**
13
- * Escape HTML special characters
14
- *
15
- * @param text - Text to escape
16
- * @returns Escaped text safe for HTML insertion
17
- */
18
- export function escapeHtml(text: string): string {
19
- return text.replace(/[&<>"']/g, (char) => HTML_ESCAPE_MAP[char] || char)
20
- }
21
-
22
- /**
23
- * Allowed HTML tags for sanitization
24
- */
25
- const ALLOWED_TAGS = new Set([
26
- 'a',
27
- 'abbr',
28
- 'b',
29
- 'blockquote',
30
- 'br',
31
- 'code',
32
- 'dd',
33
- 'del',
34
- 'div',
35
- 'dl',
36
- 'dt',
37
- 'em',
38
- 'h1',
39
- 'h2',
40
- 'h3',
41
- 'h4',
42
- 'h5',
43
- 'h6',
44
- 'hr',
45
- 'i',
46
- 'img',
47
- 'ins',
48
- 'kbd',
49
- 'li',
50
- 'ol',
51
- 'p',
52
- 'pre',
53
- 'q',
54
- 's',
55
- 'samp',
56
- 'small',
57
- 'span',
58
- 'strong',
59
- 'sub',
60
- 'sup',
61
- 'table',
62
- 'tbody',
63
- 'td',
64
- 'tfoot',
65
- 'th',
66
- 'thead',
67
- 'tr',
68
- 'u',
69
- 'ul',
70
- ])
71
-
72
- /**
73
- * Allowed attributes for sanitization
74
- */
75
- const ALLOWED_ATTRS = new Set([
76
- 'href',
77
- 'src',
78
- 'alt',
79
- 'title',
80
- 'class',
81
- 'id',
82
- 'name',
83
- 'target',
84
- 'rel',
85
- 'width',
86
- 'height',
87
- 'align',
88
- 'colspan',
89
- 'rowspan',
90
- ])
91
-
92
- /**
93
- * Sanitize HTML string by removing dangerous content
94
- *
95
- * @param html - HTML string to sanitize
96
- * @returns Sanitized HTML string
97
- */
98
- export function sanitizeHtml(html: string): string {
99
- let result = html
100
-
101
- // Remove script tags first
102
- result = result.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
103
-
104
- // Remove event handlers
105
- result = result.replace(/\s+on\w+\s*=\s*(?:"[^"]*"|'[^']*'|[^\s>]*)/gi, '')
106
-
107
- // Remove disallowed tags (keep content) and sanitize attributes
108
- result = result.replace(/<\/?(\w+)([^>]*)>/g, (match, tagName, attrs) => {
109
- const tag = tagName.toLowerCase()
110
- if (!ALLOWED_TAGS.has(tag)) {
111
- return ''
112
- }
113
-
114
- // For closing tags, just return them
115
- if (match.startsWith('</')) {
116
- return `</${tag}>`
117
- }
118
-
119
- // Sanitize attributes
120
- const sanitizedAttrs: string[] = []
121
- const attrRegex = /\s+([\w-]+)\s*=\s*(?:"([^"]*)"|'([^']*)'|(\S+))/g
122
- let attrMatch
123
-
124
- while ((attrMatch = attrRegex.exec(attrs)) !== null) {
125
- const [, attrName, v1, v2, v3] = attrMatch
126
- const attr = attrName.toLowerCase()
127
-
128
- if (ALLOWED_ATTRS.has(attr)) {
129
- let value = v1 ?? v2 ?? v3 ?? ''
130
-
131
- // Check for dangerous URLs in href/src
132
- if ((attr === 'href' || attr === 'src') && /^\s*javascript\s*:/i.test(value)) {
133
- value = ''
134
- }
135
-
136
- sanitizedAttrs.push(`${attr}="${value}"`)
137
- }
138
- }
139
-
140
- const attrStr = sanitizedAttrs.length > 0 ? ' ' + sanitizedAttrs.join(' ') : ''
141
- return `<${tag}${attrStr}>`
142
- })
143
-
144
- return result
145
- }
146
-
147
- /**
148
- * Strip all HTML tags from a string
149
- *
150
- * @param html - HTML string to strip
151
- * @returns Plain text without HTML tags
152
- */
153
- export function stripHtml(html: string): string {
154
- return html.replace(/<[^>]*>/g, '')
155
- }
@@ -1,43 +0,0 @@
1
- /**
2
- * Convert text to URL-safe slug
3
- *
4
- * @param text - Text to slugify
5
- * @returns URL-safe slug
6
- */
7
- export function slugify(text: string): string {
8
- return (
9
- text
10
- // Convert to lowercase
11
- .toLowerCase()
12
- // Replace Chinese characters with pinyin-like representation (keep as-is for simplicity)
13
- // Replace spaces and special chars with hyphens
14
- .replace(/[\s_]+/g, '-')
15
- // Remove characters that aren't alphanumeric, hyphens, or common CJK
16
- .replace(/[^\w\u4e00-\u9fff\u3040-\u309f\u30a0-\u30ff-]/g, '')
17
- // Replace multiple consecutive hyphens with single hyphen
18
- .replace(/-+/g, '-')
19
- // Remove leading/trailing hyphens
20
- .replace(/^-+|-+$/g, '')
21
- )
22
- }
23
-
24
- /**
25
- * Generate unique slug with counter suffix for duplicates
26
- *
27
- * @param text - Text to slugify
28
- * @param existingSlugs - Set of existing slugs to check against
29
- * @returns Unique slug
30
- */
31
- export function uniqueSlugify(text: string, existingSlugs: Set<string>): string {
32
- const baseSlug = slugify(text)
33
- let slug = baseSlug
34
- let counter = 1
35
-
36
- while (existingSlugs.has(slug)) {
37
- slug = `${baseSlug}-${counter}`
38
- counter++
39
- }
40
-
41
- existingSlugs.add(slug)
42
- return slug
43
- }
@@ -1,72 +0,0 @@
1
- import type { Root, Content, Parent, Literal } from 'mdast'
2
- import type { Node as UnistNode } from 'unist'
3
-
4
- // Re-export mdast types
5
- export type { Root, Content, Parent, Literal }
6
-
7
- /**
8
- * All possible markdown node types
9
- */
10
- export type MdNode = Root | Content
11
-
12
- /**
13
- * Root node of markdown AST
14
- */
15
- export type MdRoot = Root
16
-
17
- /**
18
- * Visitor function for AST traversal
19
- */
20
- export type Visitor<T extends UnistNode = MdNode> = (
21
- node: T,
22
- index: number | undefined,
23
- parent: Parent | undefined
24
- ) => void | boolean
25
-
26
- /**
27
- * Plugin options base interface
28
- */
29
- export interface PluginOptions {
30
- [key: string]: unknown
31
- }
32
-
33
- /**
34
- * Position information in source
35
- */
36
- export interface Position {
37
- line: number
38
- column: number
39
- offset?: number
40
- }
41
-
42
- /**
43
- * Location in source
44
- */
45
- export interface Location {
46
- start: Position
47
- end: Position
48
- }
49
-
50
- /**
51
- * Type guard to check if node is a parent node
52
- */
53
- export function isParent(node: UnistNode): node is Parent {
54
- return 'children' in node && Array.isArray((node as Parent).children)
55
- }
56
-
57
- /**
58
- * Type guard to check if node is a literal node
59
- */
60
- export function isLiteral(node: UnistNode): node is Literal {
61
- return 'value' in node && typeof (node as Literal).value === 'string'
62
- }
63
-
64
- /**
65
- * Type guard to check if node has specific type
66
- */
67
- export function isNodeType<T extends MdNode>(
68
- node: UnistNode,
69
- type: T['type']
70
- ): node is T {
71
- return node.type === type
72
- }
@@ -1,8 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.base.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src"
6
- },
7
- "include": ["src"]
8
- }