@schemd/core 0.1.1 → 0.2.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.
- package/README.md +22 -83
- package/dist/compiler.d.ts +16 -0
- package/dist/compiler.js +17 -0
- package/dist/index.d.ts +6 -8
- package/dist/index.js +5 -5
- package/dist/layout.d.ts +14 -4
- package/dist/layout.js +510 -138
- package/dist/limits.d.ts +4 -3
- package/dist/limits.js +2 -0
- package/dist/math-label.d.ts +20 -3
- package/dist/math-label.js +149 -56
- package/dist/parser.js +248 -27
- package/dist/renderer.js +184 -37
- package/dist/types.d.ts +64 -14
- package/dist/types.js +34 -1
- package/package.json +28 -8
- package/dist/marked-extension.d.ts +0 -23
- package/dist/marked-extension.js +0 -75
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@schemd/core",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Zero-dependency text-to-SVG compiler for schematics and UML.",
|
|
5
5
|
"homepage": "https://johnowolabiidogun.dev/tools/schemd/docs/overview",
|
|
6
6
|
"bugs": {
|
|
7
7
|
"url": "https://github.com/Sirneij/schemd/issues"
|
|
@@ -23,6 +23,30 @@
|
|
|
23
23
|
".": {
|
|
24
24
|
"types": "./dist/index.d.ts",
|
|
25
25
|
"import": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./parser": {
|
|
28
|
+
"types": "./dist/parser.d.ts",
|
|
29
|
+
"import": "./dist/parser.js"
|
|
30
|
+
},
|
|
31
|
+
"./renderer": {
|
|
32
|
+
"types": "./dist/renderer.d.ts",
|
|
33
|
+
"import": "./dist/renderer.js"
|
|
34
|
+
},
|
|
35
|
+
"./compiler": {
|
|
36
|
+
"types": "./dist/compiler.d.ts",
|
|
37
|
+
"import": "./dist/compiler.js"
|
|
38
|
+
},
|
|
39
|
+
"./layout": {
|
|
40
|
+
"types": "./dist/layout.d.ts",
|
|
41
|
+
"import": "./dist/layout.js"
|
|
42
|
+
},
|
|
43
|
+
"./math-label": {
|
|
44
|
+
"types": "./dist/math-label.d.ts",
|
|
45
|
+
"import": "./dist/math-label.js"
|
|
46
|
+
},
|
|
47
|
+
"./types": {
|
|
48
|
+
"types": "./dist/types.d.ts",
|
|
49
|
+
"import": "./dist/types.js"
|
|
26
50
|
}
|
|
27
51
|
},
|
|
28
52
|
"scripts": {
|
|
@@ -32,12 +56,8 @@
|
|
|
32
56
|
"test": "vitest run",
|
|
33
57
|
"test:coverage": "vitest run --coverage"
|
|
34
58
|
},
|
|
35
|
-
"peerDependencies": {
|
|
36
|
-
"marked": ">=17 <19"
|
|
37
|
-
},
|
|
38
59
|
"devDependencies": {
|
|
39
60
|
"@vitest/coverage-v8": "^4.1.10",
|
|
40
|
-
"marked": "^18.0.6",
|
|
41
61
|
"typescript": "^6.0.3",
|
|
42
62
|
"vitest": "^4.1.10"
|
|
43
63
|
},
|
|
@@ -46,11 +66,11 @@
|
|
|
46
66
|
},
|
|
47
67
|
"keywords": [
|
|
48
68
|
"schemd",
|
|
49
|
-
"marked",
|
|
50
69
|
"schematic",
|
|
51
70
|
"svg",
|
|
52
71
|
"compiler",
|
|
53
|
-
"engineering"
|
|
72
|
+
"engineering",
|
|
73
|
+
"uml"
|
|
54
74
|
],
|
|
55
75
|
"license": "MIT"
|
|
56
76
|
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Type-only Marked adapter for compiling fenced `schemd` diagrams during SSR.
|
|
3
|
-
*
|
|
4
|
-
* The module imports no Marked runtime. Its factory returns ordinary extension
|
|
5
|
-
* hooks to a host-owned parser and enforces both per-diagram compiler ceilings
|
|
6
|
-
* and cumulative limits across each Markdown document pass.
|
|
7
|
-
*
|
|
8
|
-
* @packageDocumentation
|
|
9
|
-
*/
|
|
10
|
-
import type { MarkedExtension } from 'marked';
|
|
11
|
-
import { type SchematicMarkedOptions } from './types.js';
|
|
12
|
-
/**
|
|
13
|
-
* Create a bounded Marked extension that compiles `schemd` fences into inline SVG.
|
|
14
|
-
*
|
|
15
|
-
* The returned extension keeps aggregate per-document budgets in closure state,
|
|
16
|
-
* resets them from Marked's `preprocess` hook, and recognizes only the canonical
|
|
17
|
-
* `schemd` language identifier.
|
|
18
|
-
*
|
|
19
|
-
* @param options - Output mode, accessible-title fallback, dynamic mode resolver,
|
|
20
|
-
* and trusted server-side diagnostic renderer.
|
|
21
|
-
* @returns A Marked extension suitable for a server-owned `Marked` instance.
|
|
22
|
-
*/
|
|
23
|
-
export declare function schematicMarkedExtension(options?: SchematicMarkedOptions): MarkedExtension;
|
package/dist/marked-extension.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { MAX_SCHEMATIC_COMPONENTS, MAX_SCHEMATIC_CONNECTIONS, MAX_SCHEMATIC_SOURCE_CHARACTERS, MAX_SCHEMATIC_SVG_OUTPUT_BYTES, utf8ByteLength } from './limits.js';
|
|
2
|
-
import { parseSchematic, parseSchematicFence } from './parser.js';
|
|
3
|
-
import { renderSchematic } from './renderer.js';
|
|
4
|
-
import { SchematicSyntaxError } from './types.js';
|
|
5
|
-
class SchematicDocumentBudgetError extends SchematicSyntaxError {
|
|
6
|
-
}
|
|
7
|
-
function emptyDocumentBudget() {
|
|
8
|
-
return { sourceCharacters: 0, components: 0, connections: 0, svgOutputBytes: 0 };
|
|
9
|
-
}
|
|
10
|
-
function consumeBudget(used, amount, limit, unit) {
|
|
11
|
-
if (amount > limit - used) {
|
|
12
|
-
throw new SchematicDocumentBudgetError(`Schematic document exceeds the cumulative ${limit.toLocaleString('en-US')} ${unit} limit.`);
|
|
13
|
-
}
|
|
14
|
-
return used + amount;
|
|
15
|
-
}
|
|
16
|
-
function escapeHtml(value) {
|
|
17
|
-
return value
|
|
18
|
-
.replaceAll('&', '&')
|
|
19
|
-
.replaceAll('<', '<')
|
|
20
|
-
.replaceAll('>', '>')
|
|
21
|
-
.replaceAll('"', '"')
|
|
22
|
-
.replaceAll("'", ''');
|
|
23
|
-
}
|
|
24
|
-
function defaultErrorRenderer(error, source) {
|
|
25
|
-
return `<figure class="schematic-error" role="group" aria-label="Schematic compilation error"><figcaption>${escapeHtml(error.message)}</figcaption><pre><code class="language-schemd">${escapeHtml(source)}</code></pre></figure>`;
|
|
26
|
-
}
|
|
27
|
-
export function schematicMarkedExtension(options = {}) {
|
|
28
|
-
let diagramIndex = 0;
|
|
29
|
-
let budget = emptyDocumentBudget();
|
|
30
|
-
let budgetError;
|
|
31
|
-
return {
|
|
32
|
-
hooks: {
|
|
33
|
-
preprocess(source) {
|
|
34
|
-
diagramIndex = 0;
|
|
35
|
-
budget = emptyDocumentBudget();
|
|
36
|
-
budgetError = undefined;
|
|
37
|
-
return source;
|
|
38
|
-
}
|
|
39
|
-
},
|
|
40
|
-
renderer: {
|
|
41
|
-
code(token) {
|
|
42
|
-
if (!token.lang || !/^schemd(?:\s|$)/i.test(token.lang.trim())) {
|
|
43
|
-
return false;
|
|
44
|
-
}
|
|
45
|
-
try {
|
|
46
|
-
if (budgetError)
|
|
47
|
-
throw budgetError;
|
|
48
|
-
budget.sourceCharacters = consumeBudget(budget.sourceCharacters, token.text.length, MAX_SCHEMATIC_SOURCE_CHARACTERS, 'schematic source character');
|
|
49
|
-
const fence = parseSchematicFence(token.lang, options.defaultTitle);
|
|
50
|
-
diagramIndex += 1;
|
|
51
|
-
const document = parseSchematic(token.text, fence);
|
|
52
|
-
const nextComponents = consumeBudget(budget.components, document.components.length, MAX_SCHEMATIC_COMPONENTS, 'component');
|
|
53
|
-
const nextConnections = consumeBudget(budget.connections, document.connections.length, MAX_SCHEMATIC_CONNECTIONS, 'connection');
|
|
54
|
-
const html = renderSchematic(document, {
|
|
55
|
-
...fence,
|
|
56
|
-
idPrefix: `schematic-${diagramIndex}`,
|
|
57
|
-
mode: options.resolveMode?.() ?? options.mode ?? 'default'
|
|
58
|
-
});
|
|
59
|
-
const nextSvgOutputBytes = consumeBudget(budget.svgOutputBytes, utf8ByteLength(html), MAX_SCHEMATIC_SVG_OUTPUT_BYTES, 'compiled SVG byte');
|
|
60
|
-
budget.components = nextComponents;
|
|
61
|
-
budget.connections = nextConnections;
|
|
62
|
-
budget.svgOutputBytes = nextSvgOutputBytes;
|
|
63
|
-
return html;
|
|
64
|
-
}
|
|
65
|
-
catch (error) {
|
|
66
|
-
if (!(error instanceof SchematicSyntaxError))
|
|
67
|
-
throw error;
|
|
68
|
-
if (error instanceof SchematicDocumentBudgetError)
|
|
69
|
-
budgetError = error;
|
|
70
|
-
return (options.onError ?? defaultErrorRenderer)(error, token.text);
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
}
|