@podlite/markdown 0.0.31 → 0.0.32
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.
- package/lib/index.cjs +43 -41
- package/lib/index.cjs.map +3 -3
- package/lib/index.esm.js +328 -33
- package/lib/index.esm.js.map +3 -3
- package/package.json +9 -2
- package/jest.config.js +0 -1
- package/jest.tsconfig.json +0 -1
- package/scripts/build.ts +0 -16
- package/scripts/build_esm.ts +0 -18
- package/src/index.tsx +0 -26
- package/src/tools.ts +0 -224
- package/t/parse.test.ts +0 -1854
- package/t/plugin.test.tsx +0 -232
- package/tsconfig-esm.json +0 -6
- package/tsconfig.json +0 -20
- package/tsconfig.tsbuildinfo +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podlite/markdown",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.32",
|
|
4
4
|
"description": "Bidirectional Markdown support for Podlite — parse .md files, embed Markdown blocks",
|
|
5
5
|
"main": "./lib/index.cjs",
|
|
6
6
|
"homepage": "https://podlite.org",
|
|
@@ -17,6 +17,13 @@
|
|
|
17
17
|
"type": "opencollective",
|
|
18
18
|
"url": "https://opencollective.com/podlite"
|
|
19
19
|
},
|
|
20
|
+
"files": [
|
|
21
|
+
"lib",
|
|
22
|
+
"esm",
|
|
23
|
+
"README.md",
|
|
24
|
+
"CHANGELOG.md",
|
|
25
|
+
"LICENSE"
|
|
26
|
+
],
|
|
20
27
|
"scripts": {
|
|
21
28
|
"clean": "rm -rf dist lib esm tsconfig.tsbuildinfo",
|
|
22
29
|
"test": "yarn g:jest --passWithNoTests",
|
|
@@ -47,7 +54,7 @@
|
|
|
47
54
|
"compatibility"
|
|
48
55
|
],
|
|
49
56
|
"dependencies": {
|
|
50
|
-
"@podlite/schema": "0.0.
|
|
57
|
+
"@podlite/schema": "0.0.39",
|
|
51
58
|
"react": "^16.0.0 || ^17.0.0 || ^18",
|
|
52
59
|
"react-dom": "^16.0.0 || ^17.0.0 || ^18",
|
|
53
60
|
"react-is": "^18.0.0",
|
package/jest.config.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
module.exports = require('../../jest.config')
|
package/jest.tsconfig.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{ "extends": "../../jest.tsconfig.json" }
|
package/scripts/build.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { build } from 'esbuild'
|
|
2
|
-
|
|
3
|
-
build({
|
|
4
|
-
bundle: true,
|
|
5
|
-
entryPoints: ['src/index.tsx'],
|
|
6
|
-
external: ['react', 'react-dom'],
|
|
7
|
-
minify: true,
|
|
8
|
-
format: 'cjs',
|
|
9
|
-
target: 'node14',
|
|
10
|
-
platform: 'node',
|
|
11
|
-
sourcemap: true,
|
|
12
|
-
outfile: 'lib/index.cjs',
|
|
13
|
-
}).catch(e => {
|
|
14
|
-
console.log('Build not successful', e.message)
|
|
15
|
-
process.exit(1)
|
|
16
|
-
})
|
package/scripts/build_esm.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { build, BuildOptions } from 'esbuild'
|
|
2
|
-
const opt: BuildOptions = {
|
|
3
|
-
tsconfig: 'tsconfig-esm.json',
|
|
4
|
-
}
|
|
5
|
-
build({
|
|
6
|
-
bundle: true,
|
|
7
|
-
entryPoints: ['src/index.tsx'],
|
|
8
|
-
external: ['react', 'react-dom'],
|
|
9
|
-
minify: false,
|
|
10
|
-
format: 'esm',
|
|
11
|
-
target: 'es2019',
|
|
12
|
-
sourcemap: true,
|
|
13
|
-
outfile: 'lib/index.esm.js',
|
|
14
|
-
...opt,
|
|
15
|
-
}).catch(e => {
|
|
16
|
-
console.log('Build not successful', e.message)
|
|
17
|
-
process.exit(1)
|
|
18
|
-
})
|
package/src/index.tsx
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { content as nodeContent, Plugins } from '@podlite/schema'
|
|
2
|
-
import { Plugin } from '@podlite/schema'
|
|
3
|
-
import { md2ast } from './tools'
|
|
4
|
-
|
|
5
|
-
export const plugin: Plugin = {
|
|
6
|
-
toAst: (_, processor) => (node, ctx) => {
|
|
7
|
-
if (typeof node !== 'string' && 'type' in node && 'content' in node && node.type === 'block') {
|
|
8
|
-
const content = node.content[0]
|
|
9
|
-
if (content && typeof content !== 'string' && 'location' in node && 'value' in content) {
|
|
10
|
-
const lineOffset = node.location.start.line
|
|
11
|
-
return { ...node, content: md2ast(content, { lineOffset }) }
|
|
12
|
-
}
|
|
13
|
-
return node
|
|
14
|
-
}
|
|
15
|
-
},
|
|
16
|
-
toJSX: helper => {
|
|
17
|
-
return nodeContent
|
|
18
|
-
},
|
|
19
|
-
}
|
|
20
|
-
export const PluginRegister: Plugins = {
|
|
21
|
-
Markdown: plugin, //TODO: deprecate it
|
|
22
|
-
markdown: plugin,
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
export { md2ast as parseMd } from './tools'
|
|
26
|
-
export default plugin
|
package/src/tools.ts
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { unified } from 'unified'
|
|
2
|
-
import markdown from 'remark-parse'
|
|
3
|
-
import remarkGfm from 'remark-gfm'
|
|
4
|
-
import remarkMath from 'remark-math'
|
|
5
|
-
|
|
6
|
-
import {
|
|
7
|
-
toAny,
|
|
8
|
-
AstTree,
|
|
9
|
-
BlockImage,
|
|
10
|
-
mkBlock,
|
|
11
|
-
mkFomattingCode,
|
|
12
|
-
mkFomattingCodeL,
|
|
13
|
-
mkVerbatim,
|
|
14
|
-
Location,
|
|
15
|
-
mkMarkupCodeF,
|
|
16
|
-
mkFormulaBlock,
|
|
17
|
-
} from '@podlite/schema'
|
|
18
|
-
import { mkRootBlock } from '@podlite/schema'
|
|
19
|
-
import { mkImage } from '@podlite/schema'
|
|
20
|
-
import { mkCaption } from '@podlite/schema'
|
|
21
|
-
import { mkFomattingCodeO } from '@podlite/schema'
|
|
22
|
-
|
|
23
|
-
type Md2astArgs = { lineOffset?: number }
|
|
24
|
-
export const md2ast = (src: string, { lineOffset }: Md2astArgs = { lineOffset: 0 }): AstTree => {
|
|
25
|
-
// first convert mardown to ast
|
|
26
|
-
const md_tree = unified().use(markdown).use(remarkGfm).use(remarkMath).parse(src)
|
|
27
|
-
|
|
28
|
-
// first pass : collect linkRefs
|
|
29
|
-
let definitionMap = {}
|
|
30
|
-
const applyLineOffset = (
|
|
31
|
-
(offset: number) =>
|
|
32
|
-
(location: Location): Location => {
|
|
33
|
-
const { start, end } = location
|
|
34
|
-
return {
|
|
35
|
-
start: {
|
|
36
|
-
...start,
|
|
37
|
-
line: start.line + offset,
|
|
38
|
-
},
|
|
39
|
-
end: {
|
|
40
|
-
...end,
|
|
41
|
-
line: end.line + offset,
|
|
42
|
-
},
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
)(lineOffset)
|
|
46
|
-
toAny({ processor: 1 })
|
|
47
|
-
.use({
|
|
48
|
-
'*:*': (writer, processor) => (node, ctx, interator) => {
|
|
49
|
-
if (node.children) interator(node.children, { ...ctx })
|
|
50
|
-
},
|
|
51
|
-
})
|
|
52
|
-
.use({
|
|
53
|
-
':definition': (writer, processor) => (node, ctx, interator) => {
|
|
54
|
-
definitionMap[node.identifier] = node
|
|
55
|
-
},
|
|
56
|
-
})
|
|
57
|
-
.run(md_tree)
|
|
58
|
-
|
|
59
|
-
// second stage - make Universal AST nodes
|
|
60
|
-
const uast = []
|
|
61
|
-
const res = toAny({ processor: 1 })
|
|
62
|
-
.use({
|
|
63
|
-
'*:*': (writer, processor) => (node, ctx, interator) => {
|
|
64
|
-
console.warn('[Podlite:md2ast]- unhandled MD node')
|
|
65
|
-
console.warn(JSON.stringify(node, null, 2))
|
|
66
|
-
if (node.children) interator(node.children, { ...ctx })
|
|
67
|
-
},
|
|
68
|
-
})
|
|
69
|
-
.use({
|
|
70
|
-
':root': (writer, processor) => (node, ctx, interator) => {
|
|
71
|
-
const { children, position, ...attr } = node
|
|
72
|
-
return mkRootBlock({}, interator(children, ctx))
|
|
73
|
-
},
|
|
74
|
-
':math': (writer, processor) => (node, ctx, interator) => {
|
|
75
|
-
const { value, children, position, ...attr } = node
|
|
76
|
-
return mkFormulaBlock({ value, location: applyLineOffset(position) })
|
|
77
|
-
},
|
|
78
|
-
':inlineMath': (writer, processor) => (node, ctx, interator) => {
|
|
79
|
-
const { value, children, position, ...attr } = node
|
|
80
|
-
return mkMarkupCodeF(value)
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
':heading': (writer, processor) => (node, ctx, interator) => {
|
|
84
|
-
const { children, position, ...attr } = node
|
|
85
|
-
return mkBlock(
|
|
86
|
-
{ type: 'block', level: node.depth, name: 'head', location: applyLineOffset(position) },
|
|
87
|
-
interator(children, ctx),
|
|
88
|
-
)
|
|
89
|
-
},
|
|
90
|
-
':text': (writer, processor) => (node, ctx, interator) => {
|
|
91
|
-
const { children, position, ...attr } = node
|
|
92
|
-
return node.value
|
|
93
|
-
// return mkNode({type:"text", value: node.value})
|
|
94
|
-
},
|
|
95
|
-
':list': (writer, processor) => (node, ctx, interator) => {
|
|
96
|
-
const savedListLevel = ctx['listLevel'] || 0
|
|
97
|
-
const { children, position, ...attr } = node
|
|
98
|
-
return mkBlock(
|
|
99
|
-
{ type: 'list', level: savedListLevel + 1, list: node.ordered ? 'ordered' : 'itemized' },
|
|
100
|
-
interator(children, { ...ctx, listLevel: savedListLevel + 1, ordered: node.ordered }),
|
|
101
|
-
)
|
|
102
|
-
},
|
|
103
|
-
':listItem': (writer, processor) => (node, ctx, interator) => {
|
|
104
|
-
const savedListLevel = ctx['listLevel'] || 0
|
|
105
|
-
const { children, position, checked, ...attr } = node
|
|
106
|
-
const blockAttrs: any = {
|
|
107
|
-
name: 'item',
|
|
108
|
-
type: 'block',
|
|
109
|
-
level: ctx['listLevel'],
|
|
110
|
-
location: applyLineOffset(position),
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (checked !== null && checked !== undefined) {
|
|
114
|
-
blockAttrs.checked = checked
|
|
115
|
-
blockAttrs.config = [{ name: 'checked', value: checked, type: 'boolean' }]
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
return mkBlock(blockAttrs, interator(children, { ...ctx, listLevel: savedListLevel, ordered: node.ordered }))
|
|
119
|
-
},
|
|
120
|
-
':paragraph': (writer, processor) => (node, ctx, interator) => {
|
|
121
|
-
const { children, position, ...attr } = node
|
|
122
|
-
return mkBlock(
|
|
123
|
-
{ type: 'para', location: applyLineOffset(position), text: 'text', margin: '' },
|
|
124
|
-
interator(children, { ...ctx }),
|
|
125
|
-
)
|
|
126
|
-
},
|
|
127
|
-
':linkReference': (writer, processor) => (node, ctx, interator) => {
|
|
128
|
-
const { children, position, ...attr } = node
|
|
129
|
-
const definition = definitionMap[attr.identifier]
|
|
130
|
-
const meta = definition?.url || ''
|
|
131
|
-
return mkFomattingCodeL({ meta }, interator(children, { ...ctx }))
|
|
132
|
-
},
|
|
133
|
-
':definition': (writer, processor) => (node, ctx, interator) => {
|
|
134
|
-
return null
|
|
135
|
-
},
|
|
136
|
-
':inlineCode': (writer, processor) => (node, ctx, interator) => {
|
|
137
|
-
const { children, position, ...attr } = node
|
|
138
|
-
return mkFomattingCode({ name: 'C' }, [node.value])
|
|
139
|
-
},
|
|
140
|
-
':strong': (writer, processor) => (node, ctx, interator) => {
|
|
141
|
-
const { children, position, ...attr } = node
|
|
142
|
-
return mkFomattingCode({ name: 'B' }, interator(children, { ...ctx }))
|
|
143
|
-
},
|
|
144
|
-
':emphasis': (writer, processor) => (node, ctx, interator) => {
|
|
145
|
-
const { children, position, ...attr } = node
|
|
146
|
-
return mkFomattingCode({ name: 'I' }, interator(children, { ...ctx }))
|
|
147
|
-
},
|
|
148
|
-
':link': (writer, processor) => (node, ctx, interator) => {
|
|
149
|
-
const { children, position, ...attr } = node
|
|
150
|
-
return mkFomattingCodeL({ meta: node.url }, interator(children, { ...ctx }))
|
|
151
|
-
},
|
|
152
|
-
':html': (writer, processor) => (node, ctx, interator) => {
|
|
153
|
-
const { children, position, ...attr } = node
|
|
154
|
-
return mkBlock({ type: 'block', name: 'Html', location: applyLineOffset(position) }, [mkVerbatim(node.value)])
|
|
155
|
-
},
|
|
156
|
-
':code': (writer, processor) => (node, ctx, interator) => {
|
|
157
|
-
const { children, position, lang, meta, ...attr } = node
|
|
158
|
-
if (lang === 'mermaid' || lang === 'diagram') {
|
|
159
|
-
return mkBlock({ type: 'block', name: 'Mermaid', config: [], location: applyLineOffset(position) }, [
|
|
160
|
-
mkVerbatim(node.value),
|
|
161
|
-
])
|
|
162
|
-
}
|
|
163
|
-
let config = []
|
|
164
|
-
if (lang) {
|
|
165
|
-
config.push({ name: 'lang', value: lang, type: 'string' })
|
|
166
|
-
}
|
|
167
|
-
if (meta) {
|
|
168
|
-
config.push({ name: 'meta', value: meta, type: 'string' })
|
|
169
|
-
} // filled as is from markdown
|
|
170
|
-
return mkBlock({ type: 'block', name: 'code', config, location: applyLineOffset(position) }, [
|
|
171
|
-
mkVerbatim(node.value),
|
|
172
|
-
])
|
|
173
|
-
},
|
|
174
|
-
':image':
|
|
175
|
-
(writer, processor) =>
|
|
176
|
-
(node, ctx, interator): BlockImage => {
|
|
177
|
-
const { title, alt, url, position, ...attr } = node
|
|
178
|
-
let config = []
|
|
179
|
-
const content: any[] = [mkImage(url, alt)]
|
|
180
|
-
if (title) content.push(mkCaption([title]))
|
|
181
|
-
return mkBlock({ type: 'block', name: 'Image', config, location: applyLineOffset(position) }, content)
|
|
182
|
-
},
|
|
183
|
-
':thematicBreak': (writer, processor) => (node, ctx, interator) => {
|
|
184
|
-
return null // TODO: add support for "thematic break"
|
|
185
|
-
},
|
|
186
|
-
':blockquote': (writer, processor) => (node, ctx, interator) => {
|
|
187
|
-
const { children, position, ...attr } = node
|
|
188
|
-
return mkBlock({ name: 'nested', location: applyLineOffset(position) }, interator(children, { ...ctx }))
|
|
189
|
-
},
|
|
190
|
-
//table section
|
|
191
|
-
':table': (writer, processor) => (node, ctx, interator) => {
|
|
192
|
-
const { children, position, align, ...attr } = node
|
|
193
|
-
return mkBlock(
|
|
194
|
-
{ name: 'table', location: applyLineOffset(position), align },
|
|
195
|
-
interator(children, { ...ctx, isHeaderUsed: 0 }),
|
|
196
|
-
)
|
|
197
|
-
},
|
|
198
|
-
':tableRow': (writer, processor) => (node, ctx, interator) => {
|
|
199
|
-
const { children, position, ...attr } = node
|
|
200
|
-
const isHeaderUsed = ctx.isHeaderUsed++
|
|
201
|
-
const blockAttrs: Record<string, unknown> = {
|
|
202
|
-
name: 'row',
|
|
203
|
-
location: applyLineOffset(position),
|
|
204
|
-
}
|
|
205
|
-
if (!isHeaderUsed) {
|
|
206
|
-
blockAttrs.config = [{ name: 'header', value: true, type: 'boolean' }]
|
|
207
|
-
}
|
|
208
|
-
return mkBlock(blockAttrs, interator(children, { ...ctx }))
|
|
209
|
-
},
|
|
210
|
-
':tableCell': (writer, processor) => (node, ctx, interator) => {
|
|
211
|
-
const { children, position, ...attr } = node
|
|
212
|
-
return mkBlock({ name: 'cell', location: applyLineOffset(position) }, interator(children, { ...ctx }))
|
|
213
|
-
},
|
|
214
|
-
':delete': (writer, processor) => (node, ctx, interator) => {
|
|
215
|
-
const { children, position, ...attr } = node
|
|
216
|
-
return mkFomattingCodeO(interator(children, { ...ctx }))
|
|
217
|
-
},
|
|
218
|
-
':break': (writer, processor) => (node, ctx, interator) => {
|
|
219
|
-
return null
|
|
220
|
-
},
|
|
221
|
-
})
|
|
222
|
-
.run(md_tree)
|
|
223
|
-
return res.interator
|
|
224
|
-
}
|