@oml/markdown 0.7.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 (112) hide show
  1. package/README.md +39 -0
  2. package/out/index.d.ts +2 -0
  3. package/out/index.js +4 -0
  4. package/out/index.js.map +1 -0
  5. package/out/md/index.d.ts +6 -0
  6. package/out/md/index.js +8 -0
  7. package/out/md/index.js.map +1 -0
  8. package/out/md/md-execution.d.ts +33 -0
  9. package/out/md/md-execution.js +3 -0
  10. package/out/md/md-execution.js.map +1 -0
  11. package/out/md/md-executor.d.ts +21 -0
  12. package/out/md/md-executor.js +498 -0
  13. package/out/md/md-executor.js.map +1 -0
  14. package/out/md/md-frontmatter.d.ts +4 -0
  15. package/out/md/md-frontmatter.js +48 -0
  16. package/out/md/md-frontmatter.js.map +1 -0
  17. package/out/md/md-registry.d.ts +7 -0
  18. package/out/md/md-registry.js +19 -0
  19. package/out/md/md-registry.js.map +1 -0
  20. package/out/md/md-runtime.d.ts +10 -0
  21. package/out/md/md-runtime.js +166 -0
  22. package/out/md/md-runtime.js.map +1 -0
  23. package/out/md/md-types.d.ts +40 -0
  24. package/out/md/md-types.js +3 -0
  25. package/out/md/md-types.js.map +1 -0
  26. package/out/md/md-yaml.d.ts +1 -0
  27. package/out/md/md-yaml.js +15 -0
  28. package/out/md/md-yaml.js.map +1 -0
  29. package/out/renderers/chart-renderer.d.ts +6 -0
  30. package/out/renderers/chart-renderer.js +392 -0
  31. package/out/renderers/chart-renderer.js.map +1 -0
  32. package/out/renderers/diagram-renderer.d.ts +7 -0
  33. package/out/renderers/diagram-renderer.js +2354 -0
  34. package/out/renderers/diagram-renderer.js.map +1 -0
  35. package/out/renderers/graph-renderer.d.ts +6 -0
  36. package/out/renderers/graph-renderer.js +1384 -0
  37. package/out/renderers/graph-renderer.js.map +1 -0
  38. package/out/renderers/index.d.ts +14 -0
  39. package/out/renderers/index.js +16 -0
  40. package/out/renderers/index.js.map +1 -0
  41. package/out/renderers/list-renderer.d.ts +6 -0
  42. package/out/renderers/list-renderer.js +252 -0
  43. package/out/renderers/list-renderer.js.map +1 -0
  44. package/out/renderers/matrix-renderer.d.ts +14 -0
  45. package/out/renderers/matrix-renderer.js +498 -0
  46. package/out/renderers/matrix-renderer.js.map +1 -0
  47. package/out/renderers/message-renderer.d.ts +6 -0
  48. package/out/renderers/message-renderer.js +14 -0
  49. package/out/renderers/message-renderer.js.map +1 -0
  50. package/out/renderers/registry.d.ts +9 -0
  51. package/out/renderers/registry.js +41 -0
  52. package/out/renderers/registry.js.map +1 -0
  53. package/out/renderers/renderer.d.ts +28 -0
  54. package/out/renderers/renderer.js +61 -0
  55. package/out/renderers/renderer.js.map +1 -0
  56. package/out/renderers/table-editor-renderer.d.ts +4 -0
  57. package/out/renderers/table-editor-renderer.js +9 -0
  58. package/out/renderers/table-editor-renderer.js.map +1 -0
  59. package/out/renderers/table-renderer.d.ts +95 -0
  60. package/out/renderers/table-renderer.js +1571 -0
  61. package/out/renderers/table-renderer.js.map +1 -0
  62. package/out/renderers/text-renderer.d.ts +7 -0
  63. package/out/renderers/text-renderer.js +219 -0
  64. package/out/renderers/text-renderer.js.map +1 -0
  65. package/out/renderers/tree-renderer.d.ts +4 -0
  66. package/out/renderers/tree-renderer.js +9 -0
  67. package/out/renderers/tree-renderer.js.map +1 -0
  68. package/out/renderers/types.d.ts +18 -0
  69. package/out/renderers/types.js +3 -0
  70. package/out/renderers/types.js.map +1 -0
  71. package/out/renderers/wikilink-utils.d.ts +6 -0
  72. package/out/renderers/wikilink-utils.js +100 -0
  73. package/out/renderers/wikilink-utils.js.map +1 -0
  74. package/out/static/browser-runtime.bundle.js +74155 -0
  75. package/out/static/browser-runtime.bundle.js.map +7 -0
  76. package/out/static/browser-runtime.d.ts +1 -0
  77. package/out/static/browser-runtime.js +218 -0
  78. package/out/static/browser-runtime.js.map +1 -0
  79. package/out/static/index.d.ts +1 -0
  80. package/out/static/index.js +3 -0
  81. package/out/static/index.js.map +1 -0
  82. package/out/static/runtime-assets.d.ts +2 -0
  83. package/out/static/runtime-assets.js +174 -0
  84. package/out/static/runtime-assets.js.map +1 -0
  85. package/package.json +74 -0
  86. package/src/index.ts +4 -0
  87. package/src/md/index.ts +8 -0
  88. package/src/md/md-execution.ts +51 -0
  89. package/src/md/md-executor.ts +598 -0
  90. package/src/md/md-frontmatter.ts +53 -0
  91. package/src/md/md-registry.ts +22 -0
  92. package/src/md/md-runtime.ts +191 -0
  93. package/src/md/md-types.ts +48 -0
  94. package/src/md/md-yaml.ts +17 -0
  95. package/src/renderers/chart-renderer.ts +473 -0
  96. package/src/renderers/diagram-renderer.ts +2520 -0
  97. package/src/renderers/graph-renderer.ts +1653 -0
  98. package/src/renderers/index.ts +16 -0
  99. package/src/renderers/list-renderer.ts +289 -0
  100. package/src/renderers/matrix-renderer.ts +616 -0
  101. package/src/renderers/message-renderer.ts +18 -0
  102. package/src/renderers/registry.ts +45 -0
  103. package/src/renderers/renderer.ts +84 -0
  104. package/src/renderers/table-editor-renderer.ts +8 -0
  105. package/src/renderers/table-renderer.ts +1868 -0
  106. package/src/renderers/text-renderer.ts +252 -0
  107. package/src/renderers/tree-renderer.ts +7 -0
  108. package/src/renderers/types.ts +22 -0
  109. package/src/renderers/wikilink-utils.ts +108 -0
  110. package/src/static/browser-runtime.ts +249 -0
  111. package/src/static/index.ts +3 -0
  112. package/src/static/runtime-assets.ts +175 -0
@@ -0,0 +1,191 @@
1
+ // Copyright (c) 2026 Modelware. All rights reserved.
2
+
3
+ import MarkdownIt from 'markdown-it';
4
+ import type { RuleInline } from 'markdown-it/lib/parser_inline.mjs';
5
+ import { extractLeadingFrontMatter, stripLeadingFrontMatter } from './md-frontmatter.js';
6
+ import { MarkdownHandlerRegistry } from './md-registry.js';
7
+ import { parseYamlMap } from './md-yaml.js';
8
+ import type { ParsedCodeBlock, PreparedCodeBlock, PreparedMarkdownDocument } from './md-types.js';
9
+
10
+ export class MarkdownPreviewRuntime {
11
+ private readonly renderer: MarkdownIt;
12
+
13
+ constructor(private readonly handlers: MarkdownHandlerRegistry) {
14
+ this.renderer = new MarkdownIt({
15
+ html: true,
16
+ linkify: true,
17
+ typographer: false,
18
+ });
19
+ this.renderer.inline.ruler.before('link', 'wiki-link', wikilinkRule);
20
+ }
21
+
22
+ prepare(markdown: string): PreparedMarkdownDocument {
23
+ const frontMatter = extractLeadingFrontMatter(markdown);
24
+ const stripped = stripLeadingFrontMatter(markdown);
25
+ const codeBlocks = this.parseCodeBlocks(stripped);
26
+ const executableBlocks = this.resolveExecutableBlocks(codeBlocks);
27
+ const renderedHtml = this.renderer.render(stripped);
28
+
29
+ return {
30
+ markdown: stripped,
31
+ renderedHtml,
32
+ frontMatter,
33
+ contextUri: frontMatter?.contextUri,
34
+ codeBlocks,
35
+ executableBlocks
36
+ };
37
+ }
38
+
39
+ private parseCodeBlocks(markdown: string): ParsedCodeBlock[] {
40
+ const tokens = this.renderer.parse(markdown, {});
41
+ const blocks: ParsedCodeBlock[] = [];
42
+
43
+ for (const token of tokens) {
44
+ if (token.type !== 'fence') {
45
+ continue;
46
+ }
47
+ const language = parseLanguage(token.info);
48
+ const meta = parseMeta(token.info);
49
+ const lineStart = token.map?.[0] ?? 0;
50
+ const lineEnd = token.map?.[1] ?? lineStart;
51
+ const parsed = parseBlockOptions(token.content);
52
+ blocks.push({
53
+ id: buildBlockId(lineStart, lineEnd, language, parsed.content),
54
+ language,
55
+ meta,
56
+ content: parsed.content,
57
+ options: parsed.options,
58
+ lineStart,
59
+ lineEnd,
60
+ });
61
+ }
62
+
63
+ return blocks;
64
+ }
65
+
66
+ private resolveExecutableBlocks(codeBlocks: ParsedCodeBlock[]): PreparedCodeBlock[] {
67
+ const executableBlocks: PreparedCodeBlock[] = [];
68
+ for (const block of codeBlocks) {
69
+ const handler = this.handlers.match(block);
70
+ if (!handler) {
71
+ continue;
72
+ }
73
+ executableBlocks.push({
74
+ block,
75
+ handlerId: handler.id,
76
+ });
77
+ }
78
+ return executableBlocks;
79
+ }
80
+ }
81
+
82
+ const wikilinkRule: RuleInline = (state, silent): boolean => {
83
+ const { src } = state;
84
+ const start = state.pos;
85
+ if (src.charCodeAt(start) !== 0x5b || src.charCodeAt(start + 1) !== 0x5b) {
86
+ return false;
87
+ }
88
+
89
+ const close = src.indexOf(']]', start + 2);
90
+ if (close < 0) {
91
+ return false;
92
+ }
93
+
94
+ const iri = src.slice(start + 2, close).trim();
95
+ if (!iri) {
96
+ return false;
97
+ }
98
+
99
+ if (!silent) {
100
+ const label = deriveWikiLabel(iri);
101
+ const open = state.push('link_open', 'a', 1);
102
+ open.attrSet('class', 'wikilink');
103
+ open.attrSet('iri', iri);
104
+ open.attrSet('href', '#');
105
+ open.attrSet('title', iri);
106
+
107
+ const text = state.push('text', '', 0);
108
+ text.content = label;
109
+
110
+ state.push('link_close', 'a', -1);
111
+ }
112
+
113
+ state.pos = close + 2;
114
+ return true;
115
+ };
116
+
117
+ function deriveWikiLabel(iri: string): string {
118
+ const normalized = iri.trim().replace(/^<|>$/g, '');
119
+ if (!normalized) {
120
+ return iri;
121
+ }
122
+ const fragment = normalized.split('#').pop() ?? '';
123
+ if (fragment && normalized.includes('#')) {
124
+ return decodeLabelPart(fragment);
125
+ }
126
+
127
+ const withoutQuery = normalized.split('?')[0];
128
+ const pathParts = withoutQuery.replace(/\/+$/, '').split('/');
129
+ const lastSegment = pathParts[pathParts.length - 1] ?? '';
130
+ if (lastSegment) {
131
+ return decodeLabelPart(lastSegment);
132
+ }
133
+ return normalized;
134
+ }
135
+
136
+ function decodeLabelPart(value: string): string {
137
+ try {
138
+ return decodeURIComponent(value);
139
+ } catch {
140
+ return value;
141
+ }
142
+ }
143
+
144
+ function parseBlockOptions(content: string): { content: string; options?: Record<string, unknown> } {
145
+ const normalized = content.replace(/^\uFEFF/, '');
146
+ const match = /^(---\s*\r?\n[\s\S]*?\r?\n---\s*\r?\n?)([\s\S]*)$/.exec(normalized);
147
+ if (!match) {
148
+ return { content };
149
+ }
150
+ const rawOptions = match[1]
151
+ .replace(/^---\s*\r?\n/, '')
152
+ .replace(/\r?\n---\s*\r?\n?$/, '');
153
+ const options = parseYamlMap(rawOptions) ?? {};
154
+ return {
155
+ content: match[2],
156
+ options: Object.keys(options).length > 0 ? options : undefined,
157
+ };
158
+ }
159
+
160
+ function parseLanguage(info: string): string {
161
+ if (!info.trim()) {
162
+ return '';
163
+ }
164
+ const first = info.trim().split(/\s+/)[0];
165
+ return first.toLowerCase();
166
+ }
167
+
168
+ function parseMeta(info: string): string | undefined {
169
+ const trimmed = info.trim();
170
+ if (!trimmed) {
171
+ return undefined;
172
+ }
173
+ const index = trimmed.indexOf(' ');
174
+ if (index === -1) {
175
+ return undefined;
176
+ }
177
+ return trimmed.slice(index + 1).trim() || undefined;
178
+ }
179
+
180
+ function buildBlockId(lineStart: number, lineEnd: number, language: string, content: string): string {
181
+ return `block-${lineStart}-${lineEnd}-${hash32(`${language}|${content}`)}`;
182
+ }
183
+
184
+ function hash32(input: string): string {
185
+ let hash = 2166136261;
186
+ for (let i = 0; i < input.length; i++) {
187
+ hash ^= input.charCodeAt(i);
188
+ hash = Math.imul(hash, 16777619);
189
+ }
190
+ return (hash >>> 0).toString(16);
191
+ }
@@ -0,0 +1,48 @@
1
+ // Copyright (c) 2026 Modelware. All rights reserved.
2
+
3
+ export interface ParsedCodeBlock {
4
+ id: string;
5
+ language: string;
6
+ meta?: string;
7
+ content: string;
8
+ options?: Record<string, unknown>;
9
+ lineStart: number;
10
+ lineEnd: number;
11
+ }
12
+
13
+ export interface MarkdownFrontMatter {
14
+ raw: string;
15
+ contextUri?: string;
16
+ data?: Record<string, unknown>;
17
+ }
18
+
19
+ export interface PreparedCodeBlock {
20
+ block: ParsedCodeBlock;
21
+ handlerId: string;
22
+ }
23
+
24
+ export interface PreparedMarkdownDocument {
25
+ markdown: string;
26
+ renderedHtml: string;
27
+ frontMatter?: MarkdownFrontMatter;
28
+ contextUri?: string;
29
+ codeBlocks: ParsedCodeBlock[];
30
+ executableBlocks: PreparedCodeBlock[];
31
+ }
32
+
33
+ export interface MarkdownExecutionContext {
34
+ documentUri: string;
35
+ modelUri?: string;
36
+ }
37
+
38
+ export interface CodeBlockExecutionResult {
39
+ ok: boolean;
40
+ output?: string;
41
+ error?: string;
42
+ }
43
+
44
+ export interface CodeBlockHandler {
45
+ id: string;
46
+ supports(block: ParsedCodeBlock): boolean;
47
+ execute(context: MarkdownExecutionContext, block: ParsedCodeBlock, signal: AbortSignal): Promise<CodeBlockExecutionResult>;
48
+ }
@@ -0,0 +1,17 @@
1
+ // Copyright (c) 2026 Modelware. All rights reserved.
2
+
3
+ import { parse } from 'yaml';
4
+
5
+ export function parseYamlMap(input: string): Record<string, unknown> | undefined {
6
+ try {
7
+ const parsed = parse(input);
8
+ return isPlainObject(parsed) ? parsed : undefined;
9
+ } catch {
10
+ return undefined;
11
+ }
12
+ }
13
+
14
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
15
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
16
+ }
17
+