@podlite/markdown 0.0.4 → 0.0.6
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/CHANGELOG.md +16 -0
- package/LICENSE +1 -1
- package/README.md +32 -6
- package/index.js +1 -1
- package/jest.config.js +0 -1
- package/jest.tsconfig.json +1 -1
- package/lib/index.cjs +40 -0
- package/lib/index.cjs.map +7 -0
- package/lib/index.esm.js +30171 -0
- package/lib/index.esm.js.map +7 -0
- package/package.json +17 -11
- package/scripts/build.ts +6 -6
- package/scripts/build_esm.ts +12 -9
- package/src/index.tsx +20 -25
- package/src/tools.ts +180 -136
- package/t/parse.test.ts +120 -123
- package/t/plugin.test.tsx +39 -47
- package/tsconfig-esm.json +6 -0
- package/tsconfig.json +2 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/esm/index.d.ts +0 -6
- package/esm/index.js +0 -39
- package/esm/index.js.map +0 -7
- package/esm/tools.d.ts +0 -6
- package/lib/index.js +0 -40
- package/lib/index.js.map +0 -7
package/package.json
CHANGED
|
@@ -1,38 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podlite/markdown",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.6",
|
|
4
4
|
"description": "=Markdown - markdown text block",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
|
-
"module": "
|
|
7
|
+
"module": "lib/index.esm.js",
|
|
8
8
|
"license": "MIT",
|
|
9
9
|
"scripts": {
|
|
10
10
|
"clean": "rm -rf dist lib esm tsconfig.tsbuildinfo",
|
|
11
11
|
"test": "yarn g:jest --passWithNoTests",
|
|
12
12
|
"build": "run-p build:cjs build:esm",
|
|
13
13
|
"build:cjs": "ts-node ./scripts/build.ts && tsc --declaration --emitDeclarationOnly",
|
|
14
|
-
"build:esm": "ts-node ./scripts/build_esm.ts
|
|
14
|
+
"build:esm": "ts-node ./scripts/build_esm.ts"
|
|
15
15
|
},
|
|
16
16
|
"publishConfig": {
|
|
17
17
|
"access": "public",
|
|
18
|
-
"main": "index.js",
|
|
18
|
+
"main": "./lib/index.js",
|
|
19
19
|
"types": "./lib/index.d.ts"
|
|
20
20
|
},
|
|
21
21
|
"peerDependencies": {
|
|
22
|
-
"react": "^16.0.0 || ^17.0.0",
|
|
22
|
+
"react": "^16.0.0 || ^17.0.0 ",
|
|
23
23
|
"react-dom": "^16.0.0 || ^17.0.0"
|
|
24
24
|
},
|
|
25
|
+
"keywords": [
|
|
26
|
+
"markdown",
|
|
27
|
+
"podlite",
|
|
28
|
+
"markup",
|
|
29
|
+
"markup-language"
|
|
30
|
+
],
|
|
25
31
|
"dependencies": {
|
|
26
|
-
"@podlite/schema": "0.0.
|
|
27
|
-
"
|
|
28
|
-
"react": "^16.0.0 || ^17.0.0",
|
|
32
|
+
"@podlite/schema": "0.0.13",
|
|
33
|
+
"react": "^16.0.0 || ^17.0.0 ",
|
|
29
34
|
"react-dom": "^16.0.0 || ^17.0.0",
|
|
30
|
-
"react-is": "^17.0.
|
|
35
|
+
"react-is": "^17.0.0",
|
|
31
36
|
"remark-gfm": "^3.0.1",
|
|
32
37
|
"remark-parse": "^10.0.1",
|
|
33
38
|
"unified": "^10.1.2"
|
|
34
39
|
},
|
|
35
40
|
"devDependencies": {
|
|
36
|
-
"esbuild": "^0.15.16"
|
|
41
|
+
"esbuild": "^0.15.16",
|
|
42
|
+
"npm-run-all": "^4.1.5"
|
|
37
43
|
}
|
|
38
44
|
}
|
package/scripts/build.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { build } from 'esbuild'
|
|
1
|
+
import { build } from 'esbuild'
|
|
2
2
|
|
|
3
3
|
build({
|
|
4
4
|
bundle: true,
|
|
@@ -9,8 +9,8 @@ build({
|
|
|
9
9
|
target: 'node14',
|
|
10
10
|
platform: 'node',
|
|
11
11
|
sourcemap: true,
|
|
12
|
-
outfile: 'lib/index.
|
|
13
|
-
}).catch(
|
|
14
|
-
console.log('Build not successful', e.message)
|
|
15
|
-
process.exit(1)
|
|
16
|
-
})
|
|
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
CHANGED
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import { build } from 'esbuild'
|
|
2
|
-
|
|
1
|
+
import { build, BuildOptions } from 'esbuild'
|
|
2
|
+
const opt: BuildOptions = {
|
|
3
|
+
tsconfig: 'tsconfig-esm.json',
|
|
4
|
+
}
|
|
3
5
|
build({
|
|
4
6
|
bundle: true,
|
|
5
7
|
entryPoints: ['src/index.tsx'],
|
|
6
8
|
external: ['react', 'react-dom'],
|
|
7
|
-
minify:
|
|
9
|
+
minify: false,
|
|
8
10
|
format: 'esm',
|
|
9
|
-
target: '
|
|
11
|
+
target: 'es2019',
|
|
10
12
|
sourcemap: true,
|
|
11
|
-
outfile: '
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
CHANGED
|
@@ -1,30 +1,25 @@
|
|
|
1
|
-
import { Plugins } from '@podlite/schema'
|
|
2
|
-
import {Plugin} from '@podlite/schema'
|
|
3
|
-
import { md2ast } from './tools'
|
|
4
|
-
import { content as nodeContent } from 'pod6/built/helpers/handlers'
|
|
5
|
-
|
|
6
|
-
export const plugin:Plugin =({
|
|
7
|
-
toAst: (_, processor) => (node, ctx) => {
|
|
8
|
-
if (typeof node !== "string" && 'type' in node && 'content' in node && node.type === 'block') {
|
|
9
|
-
const content = node.content[0]
|
|
10
|
-
if (content && typeof content !== "string" && 'location' in node && 'value' in content) {
|
|
11
|
-
const lineOffset = node.location.start.line
|
|
12
|
-
return { ...node, content: md2ast(content, {lineOffset}) }
|
|
13
|
-
}
|
|
14
|
-
return node
|
|
15
|
-
}
|
|
1
|
+
import { content as nodeContent, Plugins } from '@podlite/schema'
|
|
2
|
+
import { Plugin } from '@podlite/schema'
|
|
3
|
+
import { md2ast } from './tools'
|
|
16
4
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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,
|
|
25
22
|
}
|
|
26
23
|
|
|
27
24
|
export { md2ast as parseMd } from './tools'
|
|
28
25
|
export default plugin
|
|
29
|
-
|
|
30
|
-
|
package/src/tools.ts
CHANGED
|
@@ -1,147 +1,191 @@
|
|
|
1
|
-
import
|
|
2
|
-
import markdown
|
|
1
|
+
import { unified } from 'unified'
|
|
2
|
+
import markdown from 'remark-parse'
|
|
3
3
|
import remarkGfm from 'remark-gfm'
|
|
4
|
-
import
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
toAny,
|
|
6
|
+
AstTree,
|
|
7
|
+
BlockImage,
|
|
8
|
+
mkBlock,
|
|
9
|
+
mkFomattingCode,
|
|
10
|
+
mkFomattingCodeL,
|
|
11
|
+
mkVerbatim,
|
|
12
|
+
Location,
|
|
13
|
+
} from '@podlite/schema'
|
|
6
14
|
import { mkRootBlock } from '@podlite/schema'
|
|
7
15
|
import { mkImage } from '@podlite/schema'
|
|
8
16
|
import { mkCaption } from '@podlite/schema'
|
|
9
17
|
import { mkFomattingCodeDelete } from '@podlite/schema'
|
|
10
18
|
|
|
11
|
-
type Md2astArgs = { lineOffset?: number}
|
|
12
|
-
export const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
19
|
+
type Md2astArgs = { lineOffset?: number }
|
|
20
|
+
export const md2ast = (src: string, { lineOffset }: Md2astArgs = { lineOffset: 0 }): AstTree => {
|
|
21
|
+
// first convert mardown to ast
|
|
22
|
+
const md_tree = unified().use(markdown).use(remarkGfm).parse(src)
|
|
23
|
+
// first pass : collect linkRefs
|
|
24
|
+
let definitionMap = {}
|
|
25
|
+
const applyLineOffset = (
|
|
26
|
+
(offset: number) =>
|
|
27
|
+
(location: Location): Location => {
|
|
28
|
+
const { start, end } = location
|
|
29
|
+
return {
|
|
30
|
+
start: {
|
|
31
|
+
...start,
|
|
32
|
+
line: start.line + offset,
|
|
33
|
+
},
|
|
34
|
+
end: {
|
|
35
|
+
...end,
|
|
36
|
+
line: end.line + offset,
|
|
37
|
+
},
|
|
38
|
+
}
|
|
28
39
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
.use({
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
40
|
+
)(lineOffset)
|
|
41
|
+
toAny({ processor: 1 })
|
|
42
|
+
.use({
|
|
43
|
+
'*:*': (writer, processor) => (node, ctx, interator) => {
|
|
44
|
+
if (node.children) interator(node.children, { ...ctx })
|
|
45
|
+
},
|
|
46
|
+
})
|
|
47
|
+
.use({
|
|
48
|
+
':definition': (writer, processor) => (node, ctx, interator) => {
|
|
35
49
|
definitionMap[node.identifier] = node
|
|
36
|
-
|
|
50
|
+
},
|
|
51
|
+
})
|
|
52
|
+
.run(md_tree)
|
|
37
53
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
.use({
|
|
54
|
+
// second stage - make Universal AST nodes
|
|
55
|
+
const uast = []
|
|
56
|
+
const res = toAny({ processor: 1 })
|
|
57
|
+
.use({
|
|
58
|
+
'*:*': (writer, processor) => (node, ctx, interator) => {
|
|
42
59
|
console.warn('[Podlite:md2ast]- unhandled MD node')
|
|
43
|
-
console.warn(JSON.stringify(node,null,2))
|
|
44
|
-
if (node.children) interator(node.children, { ...ctx})
|
|
45
|
-
|
|
60
|
+
console.warn(JSON.stringify(node, null, 2))
|
|
61
|
+
if (node.children) interator(node.children, { ...ctx })
|
|
62
|
+
},
|
|
63
|
+
})
|
|
46
64
|
.use({
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
},
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
}
|
|
65
|
+
':root': (writer, processor) => (node, ctx, interator) => {
|
|
66
|
+
const { children, position, ...attr } = node
|
|
67
|
+
return mkRootBlock({}, interator(children, ctx))
|
|
68
|
+
},
|
|
69
|
+
':heading': (writer, processor) => (node, ctx, interator) => {
|
|
70
|
+
const { children, position, ...attr } = node
|
|
71
|
+
return mkBlock(
|
|
72
|
+
{ type: 'block', level: node.depth, name: 'head', location: applyLineOffset(position) },
|
|
73
|
+
interator(children, ctx),
|
|
74
|
+
)
|
|
75
|
+
},
|
|
76
|
+
':text': (writer, processor) => (node, ctx, interator) => {
|
|
77
|
+
const { children, position, ...attr } = node
|
|
78
|
+
return node.value
|
|
79
|
+
// return mkNode({type:"text", value: node.value})
|
|
80
|
+
},
|
|
81
|
+
':list': (writer, processor) => (node, ctx, interator) => {
|
|
82
|
+
const savedListLevel = ctx['listLevel'] || 0
|
|
83
|
+
const { children, position, ...attr } = node
|
|
84
|
+
return mkBlock(
|
|
85
|
+
{ type: 'list', level: savedListLevel + 1, list: node.ordered ? 'ordered' : 'itemized' },
|
|
86
|
+
interator(children, { ...ctx, listLevel: savedListLevel + 1, ordered: node.ordered }),
|
|
87
|
+
)
|
|
88
|
+
},
|
|
89
|
+
':listItem': (writer, processor) => (node, ctx, interator) => {
|
|
90
|
+
const savedListLevel = ctx['listLevel'] || 0
|
|
91
|
+
const { children, position, ...attr } = node
|
|
92
|
+
return mkBlock(
|
|
93
|
+
{ name: 'item', type: 'block', level: ctx['listLevel'], location: applyLineOffset(position) },
|
|
94
|
+
interator(children, { ...ctx, listLevel: savedListLevel, ordered: node.ordered }),
|
|
95
|
+
)
|
|
96
|
+
},
|
|
97
|
+
':paragraph': (writer, processor) => (node, ctx, interator) => {
|
|
98
|
+
const { children, position, ...attr } = node
|
|
99
|
+
return mkBlock(
|
|
100
|
+
{ type: 'para', location: applyLineOffset(position), text: 'text', margin: '' },
|
|
101
|
+
interator(children, { ...ctx }),
|
|
102
|
+
)
|
|
103
|
+
},
|
|
104
|
+
':linkReference': (writer, processor) => (node, ctx, interator) => {
|
|
105
|
+
const { children, position, ...attr } = node
|
|
106
|
+
const definition = definitionMap[attr.identifier]
|
|
107
|
+
const meta = definition?.url || ''
|
|
108
|
+
return mkFomattingCodeL({ meta }, interator(children, { ...ctx }))
|
|
109
|
+
},
|
|
110
|
+
':definition': (writer, processor) => (node, ctx, interator) => {
|
|
111
|
+
return null
|
|
112
|
+
},
|
|
113
|
+
':inlineCode': (writer, processor) => (node, ctx, interator) => {
|
|
114
|
+
const { children, position, ...attr } = node
|
|
115
|
+
return mkFomattingCode({ name: 'C' }, [node.value])
|
|
116
|
+
},
|
|
117
|
+
':strong': (writer, processor) => (node, ctx, interator) => {
|
|
118
|
+
const { children, position, ...attr } = node
|
|
119
|
+
return mkFomattingCode({ name: 'B' }, interator(children, { ...ctx }))
|
|
120
|
+
},
|
|
121
|
+
':emphasis': (writer, processor) => (node, ctx, interator) => {
|
|
122
|
+
const { children, position, ...attr } = node
|
|
123
|
+
return mkFomattingCode({ name: 'I' }, interator(children, { ...ctx }))
|
|
124
|
+
},
|
|
125
|
+
':link': (writer, processor) => (node, ctx, interator) => {
|
|
126
|
+
const { children, position, ...attr } = node
|
|
127
|
+
return mkFomattingCodeL({ meta: node.url }, interator(children, { ...ctx }))
|
|
128
|
+
},
|
|
129
|
+
':html': (writer, processor) => (node, ctx, interator) => {
|
|
130
|
+
const { children, position, ...attr } = node
|
|
131
|
+
return mkBlock({ type: 'block', name: 'Html', location: applyLineOffset(position) }, [mkVerbatim(node.value)])
|
|
132
|
+
},
|
|
133
|
+
':code': (writer, processor) => (node, ctx, interator) => {
|
|
134
|
+
const { children, position, lang, meta, ...attr } = node
|
|
135
|
+
if (lang === 'mermaid' || lang === 'diagram') {
|
|
136
|
+
return mkBlock({ type: 'block', name: 'Diagram', config: [], location: applyLineOffset(position) }, [
|
|
137
|
+
mkVerbatim(node.value),
|
|
138
|
+
])
|
|
139
|
+
}
|
|
140
|
+
let config = []
|
|
141
|
+
if (lang) {
|
|
142
|
+
config.push({ name: 'lang', value: lang, type: 'string' })
|
|
143
|
+
}
|
|
144
|
+
if (meta) {
|
|
145
|
+
config.push({ name: 'meta', value: meta, type: 'string' })
|
|
146
|
+
} // filled as is from markdown
|
|
147
|
+
return mkBlock({ type: 'block', name: 'code', config, location: applyLineOffset(position) }, [
|
|
148
|
+
mkVerbatim(node.value),
|
|
149
|
+
])
|
|
150
|
+
},
|
|
151
|
+
':image':
|
|
152
|
+
(writer, processor) =>
|
|
153
|
+
(node, ctx, interator): BlockImage => {
|
|
154
|
+
const { title, alt, url, position, ...attr } = node
|
|
155
|
+
let config = []
|
|
156
|
+
return mkBlock({ type: 'block', name: 'Image', config, location: applyLineOffset(position) }, [
|
|
157
|
+
mkImage(url, alt),
|
|
158
|
+
mkCaption([title]),
|
|
159
|
+
])
|
|
160
|
+
},
|
|
161
|
+
':thematicBreak': (writer, processor) => (node, ctx, interator) => {
|
|
162
|
+
return null // TODO: add support for "thematic break"
|
|
163
|
+
},
|
|
164
|
+
':blockquote': (writer, processor) => (node, ctx, interator) => {
|
|
165
|
+
const { children, position, ...attr } = node
|
|
166
|
+
return mkBlock({ name: 'nested', location: applyLineOffset(position) }, interator(children, { ...ctx }))
|
|
167
|
+
},
|
|
168
|
+
//table section
|
|
169
|
+
':table': (writer, processor) => (node, ctx, interator) => {
|
|
170
|
+
const { children, position, ...attr } = node
|
|
171
|
+
return mkBlock({ name: 'table', location: applyLineOffset(position) }, interator(children, { ...ctx }))
|
|
172
|
+
},
|
|
173
|
+
':tableRow': (writer, processor) => (node, ctx, interator) => {
|
|
174
|
+
const { children, position, ...attr } = node
|
|
175
|
+
return mkBlock({ name: 'table_row', location: applyLineOffset(position) }, interator(children, { ...ctx }))
|
|
176
|
+
},
|
|
177
|
+
':tableCell': (writer, processor) => (node, ctx, interator) => {
|
|
178
|
+
const { children, position, ...attr } = node
|
|
179
|
+
return mkBlock({ name: 'table_cell', location: applyLineOffset(position) }, interator(children, { ...ctx }))
|
|
180
|
+
},
|
|
181
|
+
':delete': (writer, processor) => (node, ctx, interator) => {
|
|
182
|
+
const { children, position, ...attr } = node
|
|
183
|
+
return mkFomattingCodeDelete(interator(children, { ...ctx }))
|
|
184
|
+
},
|
|
185
|
+
':break': (writer, processor) => (node, ctx, interator) => {
|
|
186
|
+
return null
|
|
187
|
+
},
|
|
188
|
+
})
|
|
189
|
+
.run(md_tree)
|
|
190
|
+
return res.interator
|
|
191
|
+
}
|