@podlite/markdown 0.0.5 → 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/package.json CHANGED
@@ -1,25 +1,25 @@
1
1
  {
2
2
  "name": "@podlite/markdown",
3
- "version": "0.0.5",
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": "esm/index.js",
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 && tsc --declaration --emitDeclarationOnly --outDir esm"
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
25
  "keywords": [
@@ -29,10 +29,10 @@
29
29
  "markup-language"
30
30
  ],
31
31
  "dependencies": {
32
- "@podlite/schema": "0.0.12",
33
- "react": "^16.0.0 || ^17.0.0",
32
+ "@podlite/schema": "0.0.13",
33
+ "react": "^16.0.0 || ^17.0.0 ",
34
34
  "react-dom": "^16.0.0 || ^17.0.0",
35
- "react-is": "^17.0.2",
35
+ "react-is": "^17.0.0",
36
36
  "remark-gfm": "^3.0.1",
37
37
  "remark-parse": "^10.0.1",
38
38
  "unified": "^10.1.2"
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.js',
13
- }).catch((e) => {
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
+ })
@@ -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: true,
9
+ minify: false,
8
10
  format: 'esm',
9
- target: 'node14',
11
+ target: 'es2019',
10
12
  sourcemap: true,
11
- outfile: 'esm/index.js',
12
- }).catch((e) => {
13
- console.log('Build not successful', e.message);
14
- process.exit(1);
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,29 +1,25 @@
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
- }
1
+ import { content as nodeContent, Plugins } from '@podlite/schema'
2
+ import { Plugin } from '@podlite/schema'
3
+ import { md2ast } from './tools'
15
4
 
16
- },
17
- toJSX: (helper) => {
18
- return nodeContent
19
- }
20
-
21
- })
22
- export const PluginRegister:Plugins = {
23
- Markdown: plugin
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,
24
22
  }
25
23
 
26
24
  export { md2ast as parseMd } from './tools'
27
25
  export default plugin
28
-
29
-
package/src/tools.ts CHANGED
@@ -1,146 +1,191 @@
1
- import {unified} from 'unified'
2
- import markdown from 'remark-parse'
1
+ import { unified } from 'unified'
2
+ import markdown from 'remark-parse'
3
3
  import remarkGfm from 'remark-gfm'
4
- import { toAny, AstTree, BlockImage, mkBlock, mkFomattingCode, mkFomattingCodeL, mkVerbatim, Location } from '@podlite/schema'
4
+ import {
5
+ toAny,
6
+ AstTree,
7
+ BlockImage,
8
+ mkBlock,
9
+ mkFomattingCode,
10
+ mkFomattingCodeL,
11
+ mkVerbatim,
12
+ Location,
13
+ } from '@podlite/schema'
5
14
  import { mkRootBlock } from '@podlite/schema'
6
15
  import { mkImage } from '@podlite/schema'
7
16
  import { mkCaption } from '@podlite/schema'
8
17
  import { mkFomattingCodeDelete } from '@podlite/schema'
9
18
 
10
- type Md2astArgs = { lineOffset?: number};
11
- export const md2ast = ( src:string, {lineOffset }:Md2astArgs = {lineOffset:0} ) :AstTree => {
12
- // first convert mardown to ast
13
- const md_tree = unified().use(markdown).use(remarkGfm).parse(src)
14
- // first pass : collect linkRefs
15
- let definitionMap = {}
16
- const applyLineOffset = ((offset:number)=>(location:Location):Location=>{
17
- const { start, end } = location
18
- return {
19
- "start": {
20
- ...start,
21
- "line": start.line + offset,
22
- },
23
- "end": {
24
- ...end,
25
- "line": end.line + offset,
26
- }
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
+ }
27
39
  }
28
- })(lineOffset);
29
- toAny({processor:1})
30
- .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
31
- if (node.children) interator(node.children, { ...ctx})
32
- }})
33
- .use({':definition' : ( writer, processor )=>( node, ctx, interator )=>{
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) => {
34
49
  definitionMap[node.identifier] = node
35
- }}).run(md_tree)
50
+ },
51
+ })
52
+ .run(md_tree)
36
53
 
37
- // second stage - make Universal AST nodes
38
- const uast = []
39
- const res = toAny({processor:1})
40
- .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
54
+ // second stage - make Universal AST nodes
55
+ const uast = []
56
+ const res = toAny({ processor: 1 })
57
+ .use({
58
+ '*:*': (writer, processor) => (node, ctx, interator) => {
41
59
  console.warn('[Podlite:md2ast]- unhandled MD node')
42
- console.warn(JSON.stringify(node,null,2))
43
- if (node.children) interator(node.children, { ...ctx})
44
- }})
60
+ console.warn(JSON.stringify(node, null, 2))
61
+ if (node.children) interator(node.children, { ...ctx })
62
+ },
63
+ })
45
64
  .use({
46
- ':root' : ( writer, processor )=>( node, ctx, interator )=>{
47
- const { children, position, ...attr} = node
48
- return mkRootBlock({}, interator(children, ctx))
49
- },
50
- ':heading' : ( writer, processor )=>( node, ctx, interator )=>{
51
- const { children, position, ...attr} = node
52
- return mkBlock({type:'block', level:node.depth,name:'head', location:applyLineOffset(position)}, interator(children, ctx));
53
- },
54
- ':text' : ( writer, processor )=>( node, ctx, interator )=>{
55
- const { children, position, ...attr} = node
56
- return node.value
57
- // return mkNode({type:"text", value: node.value})
58
- },
59
- ':list' : ( writer, processor )=>( node, ctx, interator )=>{
60
- const savedListLevel = ctx['listLevel'] || 0
61
- const { children, position, ...attr} = node
62
- return mkBlock( { type:'list', level:savedListLevel+1, list: node.ordered ? 'ordered' : 'itemized' }, interator(children, { ...ctx, listLevel : savedListLevel + 1, ordered: node.ordered}))
63
- },
64
- ':listItem' : ( writer, processor )=>( node, ctx, interator )=>{
65
- const savedListLevel = ctx['listLevel'] || 0
66
- const { children, position, ...attr} = node
67
- return mkBlock({name: 'item', type:'block', level:ctx['listLevel'], location:applyLineOffset(position)}, interator(children, { ...ctx, listLevel : savedListLevel , ordered: node.ordered}))
68
- },
69
- ':paragraph' : ( writer, processor )=>( node, ctx, interator )=>{
70
- const { children, position, ...attr} = node
71
- return mkBlock({type:'para', location:applyLineOffset(position), text: 'text', margin:"",}, interator(children, { ...ctx}))
72
- },
73
- ':linkReference' : ( writer, processor )=>( node, ctx, interator )=>{
74
- const { children, position, ...attr} = node
75
- const definition = definitionMap[attr.identifier]
76
- const meta = definition?.url || ''
77
- return mkFomattingCodeL({ meta}, interator(children, { ...ctx}))
78
- },
79
- ':definition' : ( writer, processor )=>( node, ctx, interator )=>{
80
- return null
81
- },
82
- ':inlineCode' : ( writer, processor )=>( node, ctx, interator )=>{
83
- const { children, position, ...attr} = node
84
- return mkFomattingCode({name:"C"}, [node.value])
85
- },
86
- ':strong' : ( writer, processor )=>( node, ctx, interator )=>{
87
- const { children, position, ...attr} = node
88
- return mkFomattingCode({name:"B"}, interator(children, { ...ctx}))
89
- },
90
- ':emphasis' : ( writer, processor )=>( node, ctx, interator )=>{
91
- const { children, position, ...attr} = node
92
- return mkFomattingCode({name:"I"}, interator(children, { ...ctx}))
93
- },
94
- ':link': ( writer, processor )=>( node, ctx, interator )=>{
95
- const { children, position, ...attr} = node
96
- return mkFomattingCodeL({ meta:node.url}, interator(children, { ...ctx}))
97
- },
98
- ':html': ( writer, processor )=>( node, ctx, interator )=>{
99
- const { children, position, ...attr} = node
100
- return mkBlock({type:'block', name:'Html', location:applyLineOffset(position)},[mkVerbatim(node.value)]);
101
- },
102
- ':code' : ( writer, processor )=>( node, ctx, interator )=>{
103
- const { children, position, lang, meta, ...attr} = node
104
- if (lang === 'mermaid' || lang === 'diagram') {
105
- return mkBlock({type:'block', name:'Diagram', config:[], location:applyLineOffset(position)},[mkVerbatim(node.value)]);
106
- }
107
- let config = []
108
- if (lang) { config.push({ name: 'lang', value:lang, type:"string"})}
109
- if (meta) { config.push({ name: 'meta', value:meta, type:"string"})} // filled as is from markdown
110
- return mkBlock({type:'block', name:'code', config, location:applyLineOffset(position)},[mkVerbatim(node.value)]);
111
- },
112
- ':image': ( writer, processor )=>( node, ctx, interator ):BlockImage=>{
113
- const { title, alt, url, position, ...attr} = node
114
- let config = []
115
- return mkBlock({type:'block', name:'Image', config, location:applyLineOffset(position)},[mkImage(url,alt),mkCaption([title])]);
116
- },
117
- ':thematicBreak': ( writer, processor )=>( node, ctx, interator )=>{
118
- return null // TODO: add support for "thematic break"
119
- },
120
- ':blockquote' : ( writer, processor )=>( node, ctx, interator )=>{
121
- const { children, position, ...attr} = node
122
- return mkBlock({name:'nested', location:applyLineOffset(position)}, interator(children, { ...ctx}))
123
- },
124
- //table section
125
- ':table' : ( writer, processor )=>( node, ctx, interator )=>{
126
- const { children, position, ...attr} = node
127
- return mkBlock({name:'table', location:applyLineOffset(position)}, interator(children, { ...ctx}))
128
- },
129
- ':tableRow' : ( writer, processor )=>( node, ctx, interator )=>{
130
- const { children, position, ...attr} = node
131
- return mkBlock({name:'table_row', location:applyLineOffset(position)}, interator(children, { ...ctx}))
132
- },
133
- ':tableCell' : ( writer, processor )=>( node, ctx, interator )=>{
134
- const { children, position, ...attr} = node
135
- return mkBlock({name:'table_cell', location:applyLineOffset(position)}, interator(children, { ...ctx}))
136
- },
137
- ':delete': ( writer, processor )=>( node, ctx, interator )=>{
138
- const { children, position, ...attr} = node
139
- return mkFomattingCodeDelete( interator(children, { ...ctx}))
140
- },
141
- ':break': ( writer, processor )=>( node, ctx, interator )=>{
142
- return null
143
- },
144
- }).run(md_tree)
145
- return res.interator
146
- }
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
+ }