@podlite/markdown 0.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.
package/CHANGELOG.md ADDED
@@ -0,0 +1,4 @@
1
+ # @podlite/markdown
2
+
3
+ ## 0.0.1
4
+ - initial release
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Alexandr Zahatski
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # =Markdown block
2
+
3
+ ## Contributing
4
+
5
+ This is an open source program. Feel free to fork and contribute.
6
+
7
+ In order to keep the match between this documentation and the last release, please contribute and pull requests on the dedicated develop branch.
8
+
9
+ ## AUTHOR
10
+
11
+ Copyright (c) 2022 Alexandr Zahatski
12
+
13
+ ## License
14
+
15
+ Released under a MIT License.
package/index.js ADDED
@@ -0,0 +1,2 @@
1
+ 'use strict'
2
+ module.exports = require('./lib')
package/jest.config.js ADDED
@@ -0,0 +1,2 @@
1
+
2
+ module.exports = require('../../jest.config')
@@ -0,0 +1 @@
1
+ {"extends": "../../jest.tsconfig.json"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import { Plugins } from '@podlite/schema';
2
+ import { Plugin } from '@podlite/schema';
3
+ export declare const plugin: Plugin;
4
+ export declare const PluginRegister: Plugins;
5
+ export default plugin;
package/lib/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PluginRegister = exports.plugin = void 0;
4
+ const tools_1 = require("./tools");
5
+ const handlers_1 = require("pod6/built/helpers/handlers");
6
+ exports.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: (0, tools_1.md2ast)(content, { lineOffset }) };
13
+ }
14
+ return node;
15
+ }
16
+ },
17
+ toJSX: (helper) => {
18
+ return handlers_1.content;
19
+ }
20
+ });
21
+ exports.PluginRegister = {
22
+ Markdown: exports.plugin
23
+ };
24
+ exports.default = exports.plugin;
package/lib/tools.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ import { AstTree } from '@podlite/schema';
2
+ declare type Md2astArgs = {
3
+ lineOffset?: number;
4
+ };
5
+ export declare const md2ast: (src: string, { lineOffset }?: Md2astArgs) => AstTree;
6
+ export {};
package/lib/tools.js ADDED
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.md2ast = void 0;
7
+ const unified_1 = require("unified");
8
+ const remark_parse_1 = __importDefault(require("remark-parse"));
9
+ const remark_gfm_1 = __importDefault(require("remark-gfm"));
10
+ const exportAny_1 = __importDefault(require("pod6/built/exportAny"));
11
+ const schema_1 = require("@podlite/schema");
12
+ const schema_2 = require("@podlite/schema");
13
+ const schema_3 = require("@podlite/schema");
14
+ const schema_4 = require("@podlite/schema");
15
+ const schema_5 = require("@podlite/schema");
16
+ const md2ast = (src, { lineOffset } = { lineOffset: 0 }) => {
17
+ // first convert mardown to ast
18
+ const md_tree = (0, unified_1.unified)().use(remark_parse_1.default).use(remark_gfm_1.default).parse(src);
19
+ // first pass : collect linkRefs
20
+ let definitionMap = {};
21
+ const applyLineOffset = ((offset) => (location) => {
22
+ const { start, end } = location;
23
+ return {
24
+ "start": {
25
+ ...start,
26
+ "line": start.line + offset,
27
+ },
28
+ "end": {
29
+ ...end,
30
+ "line": end.line + offset,
31
+ }
32
+ };
33
+ })(lineOffset);
34
+ (0, exportAny_1.default)({ processor: 1 })
35
+ .use({ '*:*': (writer, processor) => (node, ctx, interator) => {
36
+ if (node.children)
37
+ interator(node.children, { ...ctx });
38
+ } })
39
+ .use({ ':definition': (writer, processor) => (node, ctx, interator) => {
40
+ definitionMap[node.identifier] = node;
41
+ } }).run(md_tree);
42
+ // second stage - make Universal AST nodes
43
+ const uast = [];
44
+ const res = (0, exportAny_1.default)({ processor: 1 })
45
+ .use({ '*:*': (writer, processor) => (node, ctx, interator) => {
46
+ console.warn('[Podlite:md2ast]- unhandled MD node');
47
+ console.warn(JSON.stringify(node, null, 2));
48
+ if (node.children)
49
+ interator(node.children, { ...ctx });
50
+ } })
51
+ .use({
52
+ ':root': (writer, processor) => (node, ctx, interator) => {
53
+ const { children, position, ...attr } = node;
54
+ return (0, schema_2.mkRootBlock)({}, interator(children, ctx));
55
+ },
56
+ ':heading': (writer, processor) => (node, ctx, interator) => {
57
+ const { children, position, ...attr } = node;
58
+ return (0, schema_1.mkBlock)({ type: 'block', level: node.depth, name: 'head', location: applyLineOffset(position) }, interator(children, ctx));
59
+ },
60
+ ':text': (writer, processor) => (node, ctx, interator) => {
61
+ const { children, position, ...attr } = node;
62
+ return node.value;
63
+ // return mkNode({type:"text", value: node.value})
64
+ },
65
+ ':list': (writer, processor) => (node, ctx, interator) => {
66
+ const savedListLevel = ctx['listLevel'] || 0;
67
+ const { children, position, ...attr } = node;
68
+ return (0, schema_1.mkBlock)({ type: 'list', level: savedListLevel + 1, list: node.ordered ? 'ordered' : 'itemized' }, interator(children, { ...ctx, listLevel: savedListLevel + 1, ordered: node.ordered }));
69
+ },
70
+ ':listItem': (writer, processor) => (node, ctx, interator) => {
71
+ const savedListLevel = ctx['listLevel'] || 0;
72
+ const { children, position, ...attr } = node;
73
+ return (0, schema_1.mkBlock)({ name: 'item', type: 'block', level: ctx['listLevel'], location: applyLineOffset(position) }, interator(children, { ...ctx, listLevel: savedListLevel, ordered: node.ordered }));
74
+ },
75
+ ':paragraph': (writer, processor) => (node, ctx, interator) => {
76
+ const { children, position, ...attr } = node;
77
+ return (0, schema_1.mkBlock)({ type: 'para', location: applyLineOffset(position), text: 'text', margin: "", }, interator(children, { ...ctx }));
78
+ },
79
+ ':linkReference': (writer, processor) => (node, ctx, interator) => {
80
+ const { children, position, ...attr } = node;
81
+ const definition = definitionMap[attr.identifier];
82
+ const meta = (definition === null || definition === void 0 ? void 0 : definition.url) || '';
83
+ return (0, schema_1.mkFomattingCodeL)({ meta }, interator(children, { ...ctx }));
84
+ },
85
+ ':definition': (writer, processor) => (node, ctx, interator) => {
86
+ return null;
87
+ },
88
+ ':inlineCode': (writer, processor) => (node, ctx, interator) => {
89
+ const { children, position, ...attr } = node;
90
+ return (0, schema_1.mkFomattingCode)({ name: "C" }, [node.value]);
91
+ },
92
+ ':strong': (writer, processor) => (node, ctx, interator) => {
93
+ const { children, position, ...attr } = node;
94
+ return (0, schema_1.mkFomattingCode)({ name: "B" }, interator(children, { ...ctx }));
95
+ },
96
+ ':emphasis': (writer, processor) => (node, ctx, interator) => {
97
+ const { children, position, ...attr } = node;
98
+ return (0, schema_1.mkFomattingCode)({ name: "I" }, interator(children, { ...ctx }));
99
+ },
100
+ ':link': (writer, processor) => (node, ctx, interator) => {
101
+ const { children, position, ...attr } = node;
102
+ return (0, schema_1.mkFomattingCodeL)({ meta: node.url }, interator(children, { ...ctx }));
103
+ },
104
+ ':html': (writer, processor) => (node, ctx, interator) => {
105
+ const { children, position, ...attr } = node;
106
+ return (0, schema_1.mkBlock)({ type: 'block', name: 'Html', location: applyLineOffset(position) }, [(0, schema_1.mkVerbatim)(node.value)]);
107
+ },
108
+ ':code': (writer, processor) => (node, ctx, interator) => {
109
+ const { children, position, lang, meta, ...attr } = node;
110
+ if (lang === 'mermaid' || lang === 'diagram') {
111
+ return (0, schema_1.mkBlock)({ type: 'block', name: 'Diagram', config: [], location: applyLineOffset(position) }, [(0, schema_1.mkVerbatim)(node.value)]);
112
+ }
113
+ let config = [];
114
+ if (lang) {
115
+ config.push({ name: 'lang', value: lang, type: "string" });
116
+ }
117
+ if (meta) {
118
+ config.push({ name: 'meta', value: meta, type: "string" });
119
+ } // filled as is from markdown
120
+ return (0, schema_1.mkBlock)({ type: 'block', name: 'code', config, location: applyLineOffset(position) }, [(0, schema_1.mkVerbatim)(node.value)]);
121
+ },
122
+ ':image': (writer, processor) => (node, ctx, interator) => {
123
+ const { title, alt, url, position, ...attr } = node;
124
+ let config = [];
125
+ return (0, schema_1.mkBlock)({ type: 'block', name: 'Image', config, location: applyLineOffset(position) }, [(0, schema_3.mkImage)(url, alt), (0, schema_4.mkCaption)([title])]);
126
+ },
127
+ ':thematicBreak': (writer, processor) => (node, ctx, interator) => {
128
+ return null; // TODO: add support for "thematic break"
129
+ },
130
+ ':blockquote': (writer, processor) => (node, ctx, interator) => {
131
+ const { children, position, ...attr } = node;
132
+ return (0, schema_1.mkBlock)({ name: 'nested', location: applyLineOffset(position) }, interator(children, { ...ctx }));
133
+ },
134
+ //table section
135
+ ':table': (writer, processor) => (node, ctx, interator) => {
136
+ const { children, position, ...attr } = node;
137
+ return (0, schema_1.mkBlock)({ name: 'table', location: applyLineOffset(position) }, interator(children, { ...ctx }));
138
+ },
139
+ ':tableRow': (writer, processor) => (node, ctx, interator) => {
140
+ const { children, position, ...attr } = node;
141
+ return (0, schema_1.mkBlock)({ name: 'table_row', location: applyLineOffset(position) }, interator(children, { ...ctx }));
142
+ },
143
+ ':tableCell': (writer, processor) => (node, ctx, interator) => {
144
+ const { children, position, ...attr } = node;
145
+ return (0, schema_1.mkBlock)({ name: 'table_cell', location: applyLineOffset(position) }, interator(children, { ...ctx }));
146
+ },
147
+ ':delete': (writer, processor) => (node, ctx, interator) => {
148
+ const { children, position, ...attr } = node;
149
+ return (0, schema_5.mkFomattingCodeDelete)(interator(children, { ...ctx }));
150
+ },
151
+ ':break': (writer, processor) => (node, ctx, interator) => {
152
+ return null;
153
+ },
154
+ }).run(md_tree);
155
+ return res.interator;
156
+ };
157
+ exports.md2ast = md2ast;
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@podlite/markdown",
3
+ "version": "0.0.1",
4
+ "description": "=Markdown - markdown text block",
5
+ "main": "index.js",
6
+ "types": "./lib/index.d.ts",
7
+ "license": "MIT",
8
+ "scripts": {
9
+ "clean": "rm -rf dist lib tsconfig.tsbuildinfo",
10
+ "build": "tsc",
11
+ "test": "yarn g:jest --passWithNoTests"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public",
15
+ "main": "index.js",
16
+ "types": "./lib/index.d.ts"
17
+ },
18
+ "peerDependencies": {
19
+ "react": "^16.0.0 || ^17.0.0",
20
+ "react-dom": "^16.0.0 || ^17.0.0",
21
+ "remark-gfm": "*",
22
+ "remark-parse": "*",
23
+ "unified": "*"
24
+ },
25
+ "dependencies": {
26
+ "@podlite/schema": "0.0.10",
27
+ "pod6": "^0.0.48",
28
+ "react": "^16.0.0 || ^17.0.0",
29
+ "react-dom": "^16.0.0 || ^17.0.0",
30
+ "react-is": "^17.0.2",
31
+ "remark-gfm": "^3.0.1",
32
+ "remark-parse": "^10.0.1",
33
+ "unified": "^10.1.2"
34
+ }
35
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,28 @@
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
+ }
16
+
17
+ },
18
+ toJSX: (helper) => {
19
+ return nodeContent
20
+ }
21
+
22
+ })
23
+ export const PluginRegister:Plugins = {
24
+ Markdown: plugin
25
+ }
26
+ export default plugin
27
+
28
+
package/src/tools.ts ADDED
@@ -0,0 +1,147 @@
1
+ import {unified} from 'unified'
2
+ import markdown from 'remark-parse'
3
+ import remarkGfm from 'remark-gfm'
4
+ import toAny from 'pod6/built/exportAny'
5
+ import { AstTree, BlockImage, mkBlock, mkFomattingCode, mkFomattingCodeL, mkVerbatim, Location } from '@podlite/schema'
6
+ import { mkRootBlock } from '@podlite/schema'
7
+ import { mkImage } from '@podlite/schema'
8
+ import { mkCaption } from '@podlite/schema'
9
+ import { mkFomattingCodeDelete } from '@podlite/schema'
10
+
11
+ type Md2astArgs = { lineOffset?: number};
12
+ export const md2ast = ( src:string, {lineOffset }:Md2astArgs = {lineOffset:0} ) :AstTree => {
13
+ // first convert mardown to ast
14
+ const md_tree = unified().use(markdown).use(remarkGfm).parse(src)
15
+ // first pass : collect linkRefs
16
+ let definitionMap = {}
17
+ const applyLineOffset = ((offset:number)=>(location:Location):Location=>{
18
+ const { start, end } = location
19
+ return {
20
+ "start": {
21
+ ...start,
22
+ "line": start.line + offset,
23
+ },
24
+ "end": {
25
+ ...end,
26
+ "line": end.line + offset,
27
+ }
28
+ }
29
+ })(lineOffset);
30
+ toAny({processor:1})
31
+ .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
32
+ if (node.children) interator(node.children, { ...ctx})
33
+ }})
34
+ .use({':definition' : ( writer, processor )=>( node, ctx, interator )=>{
35
+ definitionMap[node.identifier] = node
36
+ }}).run(md_tree)
37
+
38
+ // second stage - make Universal AST nodes
39
+ const uast = []
40
+ const res = toAny({processor:1})
41
+ .use({'*:*':( writer, processor )=>( node, ctx, interator )=>{
42
+ console.warn('[Podlite:md2ast]- unhandled MD node')
43
+ console.warn(JSON.stringify(node,null,2))
44
+ if (node.children) interator(node.children, { ...ctx})
45
+ }})
46
+ .use({
47
+ ':root' : ( writer, processor )=>( node, ctx, interator )=>{
48
+ const { children, position, ...attr} = node
49
+ return mkRootBlock({}, interator(children, ctx))
50
+ },
51
+ ':heading' : ( writer, processor )=>( node, ctx, interator )=>{
52
+ const { children, position, ...attr} = node
53
+ return mkBlock({type:'block', level:node.depth,name:'head', location:applyLineOffset(position)}, interator(children, ctx));
54
+ },
55
+ ':text' : ( writer, processor )=>( node, ctx, interator )=>{
56
+ const { children, position, ...attr} = node
57
+ return node.value
58
+ // return mkNode({type:"text", value: node.value})
59
+ },
60
+ ':list' : ( writer, processor )=>( node, ctx, interator )=>{
61
+ const savedListLevel = ctx['listLevel'] || 0
62
+ const { children, position, ...attr} = node
63
+ return mkBlock( { type:'list', level:savedListLevel+1, list: node.ordered ? 'ordered' : 'itemized' }, interator(children, { ...ctx, listLevel : savedListLevel + 1, ordered: node.ordered}))
64
+ },
65
+ ':listItem' : ( writer, processor )=>( node, ctx, interator )=>{
66
+ const savedListLevel = ctx['listLevel'] || 0
67
+ const { children, position, ...attr} = node
68
+ return mkBlock({name: 'item', type:'block', level:ctx['listLevel'], location:applyLineOffset(position)}, interator(children, { ...ctx, listLevel : savedListLevel , ordered: node.ordered}))
69
+ },
70
+ ':paragraph' : ( writer, processor )=>( node, ctx, interator )=>{
71
+ const { children, position, ...attr} = node
72
+ return mkBlock({type:'para', location:applyLineOffset(position), text: 'text', margin:"",}, interator(children, { ...ctx}))
73
+ },
74
+ ':linkReference' : ( writer, processor )=>( node, ctx, interator )=>{
75
+ const { children, position, ...attr} = node
76
+ const definition = definitionMap[attr.identifier]
77
+ const meta = definition?.url || ''
78
+ return mkFomattingCodeL({ meta}, interator(children, { ...ctx}))
79
+ },
80
+ ':definition' : ( writer, processor )=>( node, ctx, interator )=>{
81
+ return null
82
+ },
83
+ ':inlineCode' : ( writer, processor )=>( node, ctx, interator )=>{
84
+ const { children, position, ...attr} = node
85
+ return mkFomattingCode({name:"C"}, [node.value])
86
+ },
87
+ ':strong' : ( writer, processor )=>( node, ctx, interator )=>{
88
+ const { children, position, ...attr} = node
89
+ return mkFomattingCode({name:"B"}, interator(children, { ...ctx}))
90
+ },
91
+ ':emphasis' : ( writer, processor )=>( node, ctx, interator )=>{
92
+ const { children, position, ...attr} = node
93
+ return mkFomattingCode({name:"I"}, interator(children, { ...ctx}))
94
+ },
95
+ ':link': ( writer, processor )=>( node, ctx, interator )=>{
96
+ const { children, position, ...attr} = node
97
+ return mkFomattingCodeL({ meta:node.url}, interator(children, { ...ctx}))
98
+ },
99
+ ':html': ( writer, processor )=>( node, ctx, interator )=>{
100
+ const { children, position, ...attr} = node
101
+ return mkBlock({type:'block', name:'Html', location:applyLineOffset(position)},[mkVerbatim(node.value)]);
102
+ },
103
+ ':code' : ( writer, processor )=>( node, ctx, interator )=>{
104
+ const { children, position, lang, meta, ...attr} = node
105
+ if (lang === 'mermaid' || lang === 'diagram') {
106
+ return mkBlock({type:'block', name:'Diagram', config:[], location:applyLineOffset(position)},[mkVerbatim(node.value)]);
107
+ }
108
+ let config = []
109
+ if (lang) { config.push({ name: 'lang', value:lang, type:"string"})}
110
+ if (meta) { config.push({ name: 'meta', value:meta, type:"string"})} // filled as is from markdown
111
+ return mkBlock({type:'block', name:'code', config, location:applyLineOffset(position)},[mkVerbatim(node.value)]);
112
+ },
113
+ ':image': ( writer, processor )=>( node, ctx, interator ):BlockImage=>{
114
+ const { title, alt, url, position, ...attr} = node
115
+ let config = []
116
+ return mkBlock({type:'block', name:'Image', config, location:applyLineOffset(position)},[mkImage(url,alt),mkCaption([title])]);
117
+ },
118
+ ':thematicBreak': ( writer, processor )=>( node, ctx, interator )=>{
119
+ return null // TODO: add support for "thematic break"
120
+ },
121
+ ':blockquote' : ( writer, processor )=>( node, ctx, interator )=>{
122
+ const { children, position, ...attr} = node
123
+ return mkBlock({name:'nested', location:applyLineOffset(position)}, interator(children, { ...ctx}))
124
+ },
125
+ //table section
126
+ ':table' : ( writer, processor )=>( node, ctx, interator )=>{
127
+ const { children, position, ...attr} = node
128
+ return mkBlock({name:'table', location:applyLineOffset(position)}, interator(children, { ...ctx}))
129
+ },
130
+ ':tableRow' : ( writer, processor )=>( node, ctx, interator )=>{
131
+ const { children, position, ...attr} = node
132
+ return mkBlock({name:'table_row', location:applyLineOffset(position)}, interator(children, { ...ctx}))
133
+ },
134
+ ':tableCell' : ( writer, processor )=>( node, ctx, interator )=>{
135
+ const { children, position, ...attr} = node
136
+ return mkBlock({name:'table_cell', location:applyLineOffset(position)}, interator(children, { ...ctx}))
137
+ },
138
+ ':delete': ( writer, processor )=>( node, ctx, interator )=>{
139
+ const { children, position, ...attr} = node
140
+ return mkFomattingCodeDelete( interator(children, { ...ctx}))
141
+ },
142
+ ':break': ( writer, processor )=>( node, ctx, interator )=>{
143
+ return null
144
+ },
145
+ }).run(md_tree)
146
+ return res.interator
147
+ }