@dooc-dev/dooc 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/dist/cli.js +157 -58
- package/dist/code-worker.global.js +152 -0
- package/dist/code.global.js +101 -101
- package/dist/compiler/asset-references.d.ts +4 -0
- package/dist/compiler/asset-references.js +117 -0
- package/dist/compiler/bundle-urls.d.ts +1 -1
- package/dist/compiler/bundle-urls.js +17 -5
- package/dist/compiler/check.js +10 -5
- package/dist/compiler/code-languages.js +7 -2
- package/dist/compiler/compile.d.ts +5 -0
- package/dist/compiler/compile.js +117 -12
- package/dist/compiler/config.js +2 -1
- package/dist/compiler/dxf.d.ts +1 -0
- package/dist/compiler/dxf.js +81 -0
- package/dist/compiler/export.d.ts +4 -0
- package/dist/compiler/export.js +147 -0
- package/dist/compiler/figures.js +43 -10
- package/dist/compiler/files.js +82 -41
- package/dist/compiler/fonts.js +4 -1
- package/dist/compiler/icons.d.ts +1 -1
- package/dist/compiler/icons.js +11 -2
- package/dist/compiler/ignored-files.d.ts +5 -0
- package/dist/compiler/ignored-files.js +98 -0
- package/dist/compiler/locales.js +13 -8
- package/dist/compiler/ops.d.ts +6 -0
- package/dist/compiler/ops.js +99 -8
- package/dist/compiler/subformat-schema.d.ts +14 -0
- package/dist/compiler/subformat-schema.js +92 -0
- package/dist/compiler/subformats.d.ts +22 -0
- package/dist/compiler/subformats.js +339 -0
- package/dist/compiler/textfile.js +3 -0
- package/dist/compiler/theme-variants.js +5 -2
- package/dist/core/code-file.js +7 -5
- package/dist/core/code-grammars.d.ts +5 -0
- package/dist/core/code-grammars.js +34 -0
- package/dist/core/content-metadata.d.ts +16 -6
- package/dist/core/content-metadata.js +283 -17
- package/dist/core/content-types.d.ts +4 -2
- package/dist/core/escape-html.d.ts +1 -0
- package/dist/core/escape-html.js +1 -0
- package/dist/core/figure-frame.js +20 -2
- package/dist/core/file-embed.js +7 -3
- package/dist/core/files.d.ts +2 -1
- package/dist/core/files.js +6 -8
- package/dist/core/flow-base-css.generated.js +1 -1
- package/dist/core/html.d.ts +4 -1
- package/dist/core/html.js +11 -10
- package/dist/core/icons.d.ts +3 -0
- package/dist/core/icons.js +3 -0
- package/dist/core/json-tools.d.ts +1 -0
- package/dist/core/json-tools.js +12 -0
- package/dist/core/language.js +5 -2
- package/dist/core/markdown.js +3 -1
- package/dist/core/previewers.d.ts +8 -0
- package/dist/core/previewers.js +2 -0
- package/dist/core/routing.d.ts +5 -2
- package/dist/core/routing.js +17 -5
- package/dist/core/schema-fields.d.ts +13 -0
- package/dist/core/schema-fields.js +175 -0
- package/dist/core/schema-graph-mount.js +26 -3
- package/dist/core/schema-refs.d.ts +8 -0
- package/dist/core/schema-refs.js +82 -0
- package/dist/core/subformat-record.d.ts +3 -0
- package/dist/core/subformat-record.js +131 -0
- package/dist/core/subformats.d.ts +43 -0
- package/dist/core/subformats.js +81 -0
- package/dist/core/toc-ui.js +26 -24
- package/dist/edit.global.js +326 -118
- package/dist/flow.global.js +3 -3
- package/dist/graph/abnfDiagram-S7VZAUFO-ASBULSKE.js +1 -0
- package/dist/graph/architecture-WOLXFQ4H-V57EDXXY.js +1 -0
- package/dist/graph/architectureDiagram-YGSQEFNW-GIMP347N.js +36 -0
- package/dist/graph/blockDiagram-APKKGZFW-RDL2T4JW.js +140 -0
- package/dist/graph/bpmnIcons-APHXDYXF-TWOSQNUQ.js +1 -0
- package/dist/graph/c4Diagram-KJ2TOQU5-75LIUC4F.js +38 -0
- package/dist/graph/chunk-26TUJSYJ.js +1 -0
- package/dist/graph/chunk-3LU66YMI.js +1 -0
- package/dist/graph/chunk-3RNHDNP5.js +1 -0
- package/dist/graph/chunk-3Y2PDVDE.js +1 -0
- package/dist/graph/chunk-3YJHMOOA.js +3 -0
- package/dist/graph/chunk-4KILFWYV.js +339 -0
- package/dist/graph/chunk-4QDE4SUK.js +1 -0
- package/dist/graph/chunk-5OO6DATX.js +1 -0
- package/dist/graph/chunk-5UUEAHTN.js +1 -0
- package/dist/graph/chunk-6T55RVVR.js +1 -0
- package/dist/graph/chunk-ARAJTKOH.js +2 -0
- package/dist/graph/chunk-BBD5CEDC.js +72 -0
- package/dist/graph/chunk-BVXT562I.js +1 -0
- package/dist/graph/chunk-BZAMCJ6B.js +32 -0
- package/dist/graph/chunk-C6J4A356.js +15 -0
- package/dist/graph/chunk-EG5UVNOC.js +213 -0
- package/dist/graph/chunk-GF4TOPUK.js +1 -0
- package/dist/graph/chunk-GYT4IYBZ.js +1 -0
- package/dist/graph/chunk-HJG2HUYJ.js +88 -0
- package/dist/graph/chunk-HOPPTA6H.js +62 -0
- package/dist/graph/chunk-I3DMZIHV.js +1 -0
- package/dist/graph/chunk-IC23EEF6.js +1 -0
- package/dist/graph/chunk-ISO77HML.js +1 -0
- package/dist/graph/chunk-JD7ZBJD2.js +1 -0
- package/dist/graph/chunk-JRSH5GNS.js +1 -0
- package/dist/graph/chunk-JSHWCPFJ.js +1 -0
- package/dist/graph/chunk-K2KLZU7M.js +10 -0
- package/dist/graph/chunk-K4I76XBX.js +14 -0
- package/dist/graph/chunk-KT4VPQD5.js +135 -0
- package/dist/graph/chunk-KU6GTEMS.js +1 -0
- package/dist/graph/chunk-LT6LXKCR.js +136 -0
- package/dist/graph/chunk-MPR575IN.js +1 -0
- package/dist/graph/chunk-N3OHKGRZ.js +1 -0
- package/dist/graph/chunk-O656ZI37.js +1 -0
- package/dist/graph/chunk-OFZLK5G4.js +1 -0
- package/dist/graph/chunk-PDH25ALM.js +2 -0
- package/dist/graph/chunk-PW2RGZ3O.js +1 -0
- package/dist/graph/chunk-QCJUF4YA.js +1 -0
- package/dist/graph/chunk-QEEA3KYY.js +2 -0
- package/dist/graph/chunk-QYAXYFNI.js +1 -0
- package/dist/graph/chunk-QYTM6GY3.js +1 -0
- package/dist/graph/chunk-R63JUMUK.js +1 -0
- package/dist/graph/chunk-SCEY3ZPT.js +1 -0
- package/dist/graph/chunk-VM2H7G2R.js +1 -0
- package/dist/graph/chunk-XQS73SBQ.js +1 -0
- package/dist/graph/chunk-YCRLJT2S.js +1 -0
- package/dist/graph/chunk-Z4FIZ7CR.js +1 -0
- package/dist/graph/chunk-ZMCB7VP2.js +1 -0
- package/dist/graph/chunk-ZSZXN4BT.js +2 -0
- package/dist/graph/classDiagram-v2-L25IHNXF-JL7LVDZH.js +217 -0
- package/dist/graph/cose-bilkent-JH36ORCC-PNM3EYPB.js +1 -0
- package/dist/graph/cynefin-EF2NZ3EQ-PSXP3DXL.js +1 -0
- package/dist/graph/cynefinDiagram-L36ESFBY-QVY54CXO.js +62 -0
- package/dist/graph/dagre-SPEB5RXH-HPGQXBZJ.js +4 -0
- package/dist/graph/diagram-3B7GL73J-DWH5WS2P.js +200 -0
- package/dist/graph/diagram-DNEMPZML-TEGH2U5I.js +24 -0
- package/dist/graph/diagram-L3MUGCDN-X4MREUWR.js +41 -0
- package/dist/graph/diagram-LA4NIPWU-NXT746HG.js +24 -0
- package/dist/graph/diagram-MHCOC4E2-JGHXSAGS.js +3 -0
- package/dist/graph/diagram-XPFHEONP-ZHZPBLT4.js +30 -0
- package/dist/graph/ebnfDiagram-UZSYDL2S-SMF6KJ43.js +1 -0
- package/dist/graph/elk-XV6XMXQE-AZ4HLCWI.js +27 -0
- package/dist/graph/erDiagram-EYBAPDEH-B3JNJ357.js +99 -0
- package/dist/graph/eventmodeling-K75KTNOO-WG6DDYMC.js +1 -0
- package/dist/graph/flowDiagram-MALGAFMO-2R7EYAQI.js +1 -0
- package/dist/graph/ganttDiagram-23REXITY-YXBYWFOJ.js +292 -0
- package/dist/graph/gitGraph-VSP46ZUC-VBFWJBBT.js +1 -0
- package/dist/graph/gitGraphDiagram-STJD7YCW-4FZDCFGV.js +106 -0
- package/dist/graph/info-OHQRW6UA-ALVDUVYV.js +1 -0
- package/dist/graph/infoDiagram-KBXO4MBG-7EHX2GQG.js +2 -0
- package/dist/graph/ishikawaDiagram-M35NCXHK-FRZD2PWQ.js +70 -0
- package/dist/graph/journeyDiagram-OPLOXDXO-7Z66JCPR.js +139 -0
- package/dist/graph/kanban-definition-BZKZHSQY-NN5AIKTE.js +89 -0
- package/dist/graph/katex-DXDNVO6V.js +257 -0
- package/dist/graph/mindmap-definition-I2QAS2VK-HDPCABN2.js +98 -0
- package/dist/graph/packet-JDAUHWVQ-5MQIBWAH.js +1 -0
- package/dist/graph/pegDiagram-FD4XPPHP-H2BFNCKT.js +1 -0
- package/dist/graph/pie-XZMESJXO-T5VRKHVT.js +1 -0
- package/dist/graph/pieDiagram-J4AKIIQI-O3NJSVD3.js +39 -0
- package/dist/graph/quadrantDiagram-XTYBLID2-TMTZJI3P.js +7 -0
- package/dist/graph/radar-ABXABTNO-C6FXIBYW.js +1 -0
- package/dist/graph/railroad-I3PHUGI6-ZSO5JXB3.js +1 -0
- package/dist/graph/railroad-abnf-LNEFI6M7-CI4GQRHB.js +1 -0
- package/dist/graph/railroad-ebnf-DWYD2UWJ-MUENNCGK.js +1 -0
- package/dist/graph/railroad-peg-JR7BVNR7-N3X5GGIX.js +1 -0
- package/dist/graph/railroadDiagram-MRQ6NCDL-JHWS23UO.js +1 -0
- package/dist/graph/requirementDiagram-Y7LWUY3S-7DVLPO4B.js +84 -0
- package/dist/graph/sankeyDiagram-WGJFYUXA-Z5L4R5TR.js +40 -0
- package/dist/graph/schema-graph-global.js +639 -0
- package/dist/graph/sequenceDiagram-5NDRBG5M-RCTU25NT.js +169 -0
- package/dist/graph/sizeCapture-INFHLROL-RROY32P5.js +1 -0
- package/dist/graph/stateDiagram-v2-FJ4FJNEA-BXUSPWDK.js +291 -0
- package/dist/graph/swimlanes-UMQ2AFYF-LDD74Q2S.js +1 -0
- package/dist/graph/swimlanesDiagram-OMPLPG4O-ZYULLJSU.js +8 -0
- package/dist/graph/timeline-definition-SEUHKTQE-ZQSMIVAS.js +120 -0
- package/dist/graph/treeView-D4JQ5SDB-42S7U73N.js +1 -0
- package/dist/graph/treemap-SAJKECNS-RUBYQ7XF.js +1 -0
- package/dist/graph/usecaseDiagram-AFA6YXN4-57RDRYXF.js +370 -0
- package/dist/graph/vennDiagram-NNMJR3RB-5C3LVAV6.js +34 -0
- package/dist/graph/wardley-7MLQ67FV-NV5HXTTC.js +1 -0
- package/dist/graph/wardleyDiagram-S7YFUSEH-6D5BKXHM.js +78 -0
- package/dist/graph/xychartDiagram-GGNKY7IX-6ET3ZM67.js +7 -0
- package/dist/mcp.js +54 -12
- package/dist/plugins/index.d.ts +1 -0
- package/dist/plugins/transport.d.ts +22 -0
- package/dist/plugins/transport.js +1 -0
- package/dist/plugins.global.js +95 -0
- package/dist/subformats.global.js +21 -0
- package/dist/ui.global.js +80 -60
- package/dist/viewer/build-plugins.d.ts +7 -0
- package/dist/viewer/build-plugins.js +70 -0
- package/dist/viewer/build.d.ts +11 -0
- package/dist/viewer/build.js +52 -11
- package/dist/viewer/code-file.js +7 -2
- package/dist/viewer/editor-tools.d.ts +7 -7
- package/dist/viewer/editor-tools.js +36 -5
- package/dist/viewer/figure-preview.d.ts +4 -0
- package/dist/viewer/figure-preview.js +54 -0
- package/dist/viewer/font-picker.js +5 -1
- package/dist/viewer/git-diff.d.ts +3 -0
- package/dist/viewer/git-diff.js +47 -0
- package/dist/viewer/hmr.js +8 -4
- package/dist/viewer/i18n.js +7 -5
- package/dist/viewer/language-picker.d.ts +2 -2
- package/dist/viewer/language-picker.js +26 -9
- package/dist/viewer/lazy-preview.d.ts +2 -0
- package/dist/viewer/lazy-preview.js +43 -0
- package/dist/viewer/locales/eng.json +95 -10
- package/dist/viewer/locales/jpn.json +96 -11
- package/dist/viewer/locales/zho.json +96 -11
- package/dist/viewer/nav-focus.js +8 -1
- package/dist/viewer/nav-update.d.ts +2 -0
- package/dist/viewer/nav-update.js +117 -0
- package/dist/viewer/plugin-blocks.js +1 -1
- package/dist/viewer/plugin-style.d.ts +1 -0
- package/dist/viewer/plugin-style.js +82 -0
- package/dist/viewer/plugins.js +1 -1
- package/dist/viewer/resize.d.ts +0 -2
- package/dist/viewer/resize.js +2 -98
- package/dist/viewer/resource-library.d.ts +11 -9
- package/dist/viewer/resource-library.js +181 -31
- package/dist/viewer/search.d.ts +4 -2
- package/dist/viewer/search.js +252 -53
- package/dist/viewer/serve.d.ts +11 -5
- package/dist/viewer/serve.js +913 -295
- package/dist/viewer/site-icon.js +13 -6
- package/dist/viewer/site-title.js +15 -2
- package/dist/viewer/textfile.d.ts +5 -0
- package/dist/viewer/textfile.js +34 -2
- package/dist/viewer/theme-picker.d.ts +2 -0
- package/dist/viewer/theme-picker.js +24 -8
- package/dist/viewer/workspace.d.ts +2 -0
- package/dist/viewer/workspace.js +46 -1
- package/package.json +27 -10
- package/dist/compiler/bundle.d.ts +0 -2
- package/dist/compiler/bundle.js +0 -100
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { parseFragment } from "parse5";
|
|
2
|
+
import postcss from "postcss";
|
|
3
|
+
import valueParser from "postcss-value-parser";
|
|
4
|
+
const cssReferences = (source, replace) => {
|
|
5
|
+
const tree = postcss.parse(source);
|
|
6
|
+
const value = (source, imports = false) => {
|
|
7
|
+
const parsed = valueParser(source);
|
|
8
|
+
parsed.walk((node) => {
|
|
9
|
+
if (node.type === "function" && node.value.toLowerCase() === "url") {
|
|
10
|
+
const before = valueParser.stringify(node.nodes).replace(/^(['"])(.*)\1$/, "$2");
|
|
11
|
+
const next = replace(before, true);
|
|
12
|
+
if (next !== before)
|
|
13
|
+
node.nodes = valueParser(JSON.stringify(next)).nodes;
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
if (node.type === "function" && /^(?:-webkit-)?image-set$/i.test(node.value))
|
|
17
|
+
for (const child of node.nodes)
|
|
18
|
+
if (child.type === "string")
|
|
19
|
+
child.value = replace(child.value, true);
|
|
20
|
+
});
|
|
21
|
+
const first = parsed.nodes.find((node) => node.type !== "space" && node.type !== "comment");
|
|
22
|
+
if (imports && first?.type === "string")
|
|
23
|
+
first.value = replace(first.value, true);
|
|
24
|
+
return parsed.toString();
|
|
25
|
+
};
|
|
26
|
+
tree.walkDecls((node) => {
|
|
27
|
+
node.value = value(node.value);
|
|
28
|
+
});
|
|
29
|
+
tree.walkAtRules((node) => {
|
|
30
|
+
node.params = value(node.params, node.name.toLowerCase() === "import");
|
|
31
|
+
});
|
|
32
|
+
return tree.toString();
|
|
33
|
+
};
|
|
34
|
+
/** Visit real asset URLs and preserve surrounding markup, examples and scripts. */
|
|
35
|
+
export const assetReferences = (source, extension, replace) => {
|
|
36
|
+
if (extension === "css")
|
|
37
|
+
return cssReferences(source, replace);
|
|
38
|
+
const changes = [];
|
|
39
|
+
const walk = (parent) => {
|
|
40
|
+
for (const node of parent.childNodes) {
|
|
41
|
+
if (!("tagName" in node))
|
|
42
|
+
continue;
|
|
43
|
+
for (const attr of node.attrs) {
|
|
44
|
+
let next = attr.value;
|
|
45
|
+
if (["src", "poster", "background"].includes(attr.name) ||
|
|
46
|
+
(attr.name === "data" && node.tagName === "object") ||
|
|
47
|
+
["href", "xlink:href"].includes(attr.name))
|
|
48
|
+
next = replace(attr.value, !["a", "area", "base"].includes(node.tagName));
|
|
49
|
+
else if (attr.name === "srcdoc")
|
|
50
|
+
next = assetReferences(attr.value, "html", replace);
|
|
51
|
+
else if (attr.name === "style")
|
|
52
|
+
next = cssReferences(attr.value, replace);
|
|
53
|
+
else if (["srcset", "imagesrcset"].includes(attr.name)) {
|
|
54
|
+
let rest = attr.value;
|
|
55
|
+
next = "";
|
|
56
|
+
while (rest) {
|
|
57
|
+
const space = /^[\s,]*/.exec(rest)?.[0] ?? "";
|
|
58
|
+
next += space;
|
|
59
|
+
rest = rest.slice(space.length);
|
|
60
|
+
const token = /^\S+/.exec(rest)?.[0];
|
|
61
|
+
if (!token)
|
|
62
|
+
break;
|
|
63
|
+
const url = token.replace(/,+$/, "");
|
|
64
|
+
next += replace(url, true) + token.slice(url.length);
|
|
65
|
+
rest = rest.slice(token.length);
|
|
66
|
+
if (!token.endsWith(",")) {
|
|
67
|
+
const descriptor = /^[^,]*,?/.exec(rest)?.[0] ?? "";
|
|
68
|
+
next += descriptor;
|
|
69
|
+
rest = rest.slice(descriptor.length);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
if (next === attr.value)
|
|
74
|
+
continue;
|
|
75
|
+
const name = attr.prefix ? `${attr.prefix}:${attr.name}` : attr.name;
|
|
76
|
+
const location = node.sourceCodeLocation?.attrs?.[name];
|
|
77
|
+
if (!location)
|
|
78
|
+
throw new Error(`Cannot update attribute: ${name}`);
|
|
79
|
+
const raw = source.slice(location.startOffset, location.endOffset);
|
|
80
|
+
const head = /^[^=]+\s*=\s*/.exec(raw)?.[0];
|
|
81
|
+
if (!head)
|
|
82
|
+
throw new Error(`Cannot update attribute: ${name}`);
|
|
83
|
+
const quote = raw[head.length] === "'" ? "'" : '"';
|
|
84
|
+
const escaped = next
|
|
85
|
+
.replaceAll("&", "&")
|
|
86
|
+
.replaceAll("<", "<")
|
|
87
|
+
.replaceAll(quote, quote === '"' ? """ : "'");
|
|
88
|
+
changes.push({
|
|
89
|
+
from: location.startOffset,
|
|
90
|
+
to: location.endOffset,
|
|
91
|
+
text: head + quote + escaped + quote,
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (node.tagName === "style")
|
|
95
|
+
for (const child of node.childNodes) {
|
|
96
|
+
if (!("value" in child))
|
|
97
|
+
continue;
|
|
98
|
+
const next = cssReferences(child.value, replace);
|
|
99
|
+
if (next === child.value)
|
|
100
|
+
continue;
|
|
101
|
+
const location = child.sourceCodeLocation;
|
|
102
|
+
if (!location)
|
|
103
|
+
throw new Error("Cannot update style content");
|
|
104
|
+
changes.push({
|
|
105
|
+
from: location.startOffset,
|
|
106
|
+
to: location.endOffset,
|
|
107
|
+
text: next,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
walk("content" in node ? node.content : node);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
walk(parseFragment(source, { sourceCodeLocationInfo: true }));
|
|
114
|
+
for (const change of changes.sort((a, b) => b.from - a.from))
|
|
115
|
+
source = source.slice(0, change.from) + change.text + source.slice(change.to);
|
|
116
|
+
return source;
|
|
117
|
+
};
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ZipEntry } from "./zip.js";
|
|
2
2
|
/** Snapshot embedded URLs and their declarative assets without changing the source bundle. */
|
|
3
|
-
export declare const embedBundleUrls: (root: string, entries: readonly ZipEntry[]) => Promise<ZipEntry[]>;
|
|
3
|
+
export declare const embedBundleUrls: (root: string, entries: readonly ZipEntry[], resolvePlugin?: (url: string) => Promise<string | undefined>) => Promise<ZipEntry[]>;
|
|
@@ -7,6 +7,7 @@ import valueParser from "postcss-value-parser";
|
|
|
7
7
|
import { embeddedImages, parseMdast } from "../core/markdown.js";
|
|
8
8
|
import { isRemoteTextfile } from "../core/textfile.js";
|
|
9
9
|
import { collectRefs } from "./dialect.js";
|
|
10
|
+
import { assertNotIgnored } from "./ignored-files.js";
|
|
10
11
|
import { readManifest } from "./manifest.js";
|
|
11
12
|
import { fetchPublicFile } from "./public-file.js";
|
|
12
13
|
import { textfilePath } from "./textfile.js";
|
|
@@ -23,6 +24,8 @@ const types = {
|
|
|
23
24
|
webp: "image/webp",
|
|
24
25
|
mp4: "video/mp4",
|
|
25
26
|
webm: "video/webm",
|
|
27
|
+
pdf: "application/pdf",
|
|
28
|
+
dxf: "image/vnd.dxf",
|
|
26
29
|
woff: "font/woff",
|
|
27
30
|
woff2: "font/woff2",
|
|
28
31
|
ttf: "font/ttf",
|
|
@@ -32,14 +35,15 @@ const types = {
|
|
|
32
35
|
const extension = (source) => posix.extname(new URL(source).pathname).slice(1).toLowerCase();
|
|
33
36
|
const decode = (data) => new TextDecoder("utf-8", { fatal: true }).decode(data);
|
|
34
37
|
/** Snapshot embedded URLs and their declarative assets without changing the source bundle. */
|
|
35
|
-
export const embedBundleUrls = async (root, entries) => {
|
|
38
|
+
export const embedBundleUrls = async (root, entries, resolvePlugin) => {
|
|
36
39
|
const result = new Map(entries.map((entry) => [entry.name, { ...entry }]));
|
|
37
40
|
const figures = new Set(entries
|
|
38
41
|
.filter((entry) => /\.(md|callouth)$/.test(entry.name))
|
|
39
42
|
.flatMap((entry) => collectRefs(parseMdast(decode(entry.data))).figures));
|
|
40
|
-
const
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
const manifest = readManifest(root);
|
|
44
|
+
for (const file of [manifest.icon, ...Object.values(manifest.themeIcons ?? {})])
|
|
45
|
+
if (file)
|
|
46
|
+
figures.add(file);
|
|
43
47
|
const cache = new Map();
|
|
44
48
|
const sources = new Map();
|
|
45
49
|
const pending = new Set();
|
|
@@ -68,6 +72,8 @@ export const embedBundleUrls = async (root, entries) => {
|
|
|
68
72
|
try {
|
|
69
73
|
let source = sources.get(key);
|
|
70
74
|
if (!source) {
|
|
75
|
+
if (url.protocol === "bundle:")
|
|
76
|
+
assertNotIgnored(root, decodeURIComponent(url.pathname.slice(1)));
|
|
71
77
|
if (url.protocol === "https:" && ++downloads > 256)
|
|
72
78
|
throw new Error("Bundle exceeds 256 remote assets");
|
|
73
79
|
let type = "";
|
|
@@ -263,7 +269,13 @@ export const embedBundleUrls = async (root, entries) => {
|
|
|
263
269
|
if (remote.ref)
|
|
264
270
|
throw new Error("Git URL embeds require a public HTTPS file URL");
|
|
265
271
|
const url = keyOf(remote.url, image.url);
|
|
266
|
-
const
|
|
272
|
+
const plugin = await resolvePlugin?.(url.href);
|
|
273
|
+
const asset = plugin === undefined
|
|
274
|
+
? await resource(url, false)
|
|
275
|
+
: {
|
|
276
|
+
data: Buffer.from(await markup(plugin, url.href)),
|
|
277
|
+
type: "text/html",
|
|
278
|
+
};
|
|
267
279
|
const ext = Object.entries(types).find(([, type]) => type === asset.type)?.[0] ??
|
|
268
280
|
(extension(url.href) || "txt");
|
|
269
281
|
const name = `files/bundle/${createHash("sha256").update(url.href).digest("hex")}.${ext}`;
|
package/dist/compiler/check.js
CHANGED
|
@@ -175,6 +175,8 @@ export const checkSite = (root, options = {}) => {
|
|
|
175
175
|
add("MANIFEST_INVALID", "error", MANIFEST_FILE, message);
|
|
176
176
|
if (manifest.errors.length)
|
|
177
177
|
return finish(report, options);
|
|
178
|
+
if (manifest.licence)
|
|
179
|
+
read(manifest.licence, { sourceFile: MANIFEST_FILE });
|
|
178
180
|
let sourceLanguage;
|
|
179
181
|
try {
|
|
180
182
|
sourceLanguage = contentLanguageCode(manifest.defaultLang ?? DEFAULT_LANG);
|
|
@@ -199,7 +201,9 @@ export const checkSite = (root, options = {}) => {
|
|
|
199
201
|
else if (!report.themes.includes(theme))
|
|
200
202
|
add("THEME_UNDECLARED", "warning", `themes/${theme}`, "Theme variant is not enabled by the site declaration and will not be displayed.", { theme });
|
|
201
203
|
}
|
|
202
|
-
const docs = new Map(flattenTree(manifest.tree)
|
|
204
|
+
const docs = new Map(flattenTree(manifest.tree)
|
|
205
|
+
.filter((node) => !node.subformat)
|
|
206
|
+
.map(({ id }) => [docFile(id, manifest.files), id]));
|
|
203
207
|
const visited = new Set();
|
|
204
208
|
const checkedThemes = new Set();
|
|
205
209
|
const checkAsset = (file, context, withThemes = true) => {
|
|
@@ -410,10 +414,11 @@ export const checkSite = (root, options = {}) => {
|
|
|
410
414
|
add("FILE_UNREADABLE", "error", file, messageOf(error));
|
|
411
415
|
}
|
|
412
416
|
}
|
|
413
|
-
for (const file of
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
+
for (const file of [
|
|
418
|
+
manifest.icon,
|
|
419
|
+
...Object.values(manifest.themeIcons ?? {}),
|
|
420
|
+
...Object.values(manifest.files?.icons ?? {}),
|
|
421
|
+
])
|
|
417
422
|
if (file)
|
|
418
423
|
checkAsset(file, { language: sourceLanguage }, false);
|
|
419
424
|
if (has("render")) {
|
|
@@ -3,6 +3,7 @@ import { join, relative, resolve } from "node:path";
|
|
|
3
3
|
import { languageAliasNames, languageNames } from "@shikijs/langs";
|
|
4
4
|
import { bundledLanguages } from "shiki/langs";
|
|
5
5
|
import { CODE_LANGS, CODE_TEMPLATES } from "../core/taxonomy.js";
|
|
6
|
+
import { ignoredFiles } from "./ignored-files.js";
|
|
6
7
|
export const CODE_LANGUAGES_DIR = ".dooc/code-languages";
|
|
7
8
|
const supported = new Set([
|
|
8
9
|
...languageNames,
|
|
@@ -79,9 +80,13 @@ export const loadCodeGrammars = async (names) => {
|
|
|
79
80
|
/** Renderer-owned registrations; Markdown content is never changed. */
|
|
80
81
|
export const loadCodeLanguages = async (root) => {
|
|
81
82
|
const directory = join(root, CODE_LANGUAGES_DIR);
|
|
83
|
+
const files = existsSync(directory)
|
|
84
|
+
? readdirSync(directory).filter((file) => file.endsWith(".json"))
|
|
85
|
+
: [];
|
|
86
|
+
const ignored = ignoredFiles(root, files.map((file) => `${CODE_LANGUAGES_DIR}/${file}`));
|
|
82
87
|
const extra = existsSync(directory)
|
|
83
|
-
?
|
|
84
|
-
.filter((file) =>
|
|
88
|
+
? files
|
|
89
|
+
.filter((file) => !ignored.has(`${CODE_LANGUAGES_DIR}/${file}`))
|
|
85
90
|
.sort()
|
|
86
91
|
.flatMap((file) => readGrammars(join(directory, file)))
|
|
87
92
|
: [];
|
|
@@ -9,6 +9,8 @@ export declare const collectDocLinks: (body: string) => string[];
|
|
|
9
9
|
/** Source text and diagnostics for editing. key = CompiledDoc.file. */
|
|
10
10
|
export interface DocSource {
|
|
11
11
|
terms: TermEntry[];
|
|
12
|
+
/** Files already discovered during parsing, used by the viewer's change watcher. */
|
|
13
|
+
referencedFiles: string[];
|
|
12
14
|
/**
|
|
13
15
|
* Raw text on disk (including front-matter). The editor's initial value and the base for
|
|
14
16
|
* detecting external changes.
|
|
@@ -28,9 +30,12 @@ export interface DocSource {
|
|
|
28
30
|
errors: string[];
|
|
29
31
|
}
|
|
30
32
|
export interface Compiled {
|
|
33
|
+
subformats?: import("../core/subformats.js").Subformat[];
|
|
31
34
|
previews: PreviewSettings;
|
|
32
35
|
title?: string;
|
|
33
36
|
icon?: string;
|
|
37
|
+
/** Theme name → site icon from themes/<theme>/<icon file>. */
|
|
38
|
+
iconVariants?: Record<string, string>;
|
|
34
39
|
/** Display order follows dooc.yaml. Invalid sources retain their diagnostics for editing. */
|
|
35
40
|
docs: CompiledDoc[];
|
|
36
41
|
terms: TermEntry[];
|
package/dist/compiler/compile.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Reads dooc.yaml and source files into validated, in-memory content for the viewer and HTML
|
|
2
2
|
// bundles. createCompiler caches unchanged files; compileSite performs a one-shot compilation.
|
|
3
|
-
import { existsSync, readdirSync } from "node:fs";
|
|
3
|
+
import { existsSync, readdirSync, statSync } from "node:fs";
|
|
4
4
|
import { join } from "node:path";
|
|
5
5
|
import { walkBlocks } from "../core/blocks.js";
|
|
6
6
|
import { docIdOfFile, flattenTree, MANIFEST_FILE } from "../core/content-metadata.js";
|
|
@@ -18,9 +18,11 @@ import { createFileCache, createSigCache, dirSig, fileSig } from "./file-cache.j
|
|
|
18
18
|
import { projectFiles } from "./files.js";
|
|
19
19
|
import { toBlocks } from "./from-mdast.js";
|
|
20
20
|
import { loadIcons, squareSvg } from "./icons.js";
|
|
21
|
+
import { assertNotIgnored, ignoredFiles } from "./ignored-files.js";
|
|
21
22
|
import { contentLanguageCode, isContentLanguage } from "./language-codes.js";
|
|
22
23
|
import { siteContentLanguages, translationFile } from "./locales.js";
|
|
23
24
|
import { parseManifestText } from "./manifest.js";
|
|
25
|
+
import { loadSubformat, parseSubformatRecord, validateSubformatRecord } from "./subformats.js";
|
|
24
26
|
import { parseTermSource } from "./terms.js";
|
|
25
27
|
import { readTextfile, selectTextfileRange } from "./textfile.js";
|
|
26
28
|
import { TextfileUnavailable } from "./textfile-remote.js";
|
|
@@ -50,6 +52,7 @@ const parseFile = (raw, previews, file) => {
|
|
|
50
52
|
const figureFiles = refs.figures;
|
|
51
53
|
return {
|
|
52
54
|
raw,
|
|
55
|
+
referencedFiles: [...refs.textfiles, ...refs.figures, ...refs.terms],
|
|
53
56
|
parsed,
|
|
54
57
|
blocks,
|
|
55
58
|
toc,
|
|
@@ -239,21 +242,35 @@ export const createCompiler = (root, options = {}) => {
|
|
|
239
242
|
!siteContentLanguages(root, manifest).includes(options.locale))
|
|
240
243
|
throw new Error(`Language ${options.locale} is not declared in dooc.yaml languages`);
|
|
241
244
|
const themes = siteThemes(root, manifest);
|
|
242
|
-
const
|
|
245
|
+
const paths = projectFiles(root);
|
|
246
|
+
const visualFiles = paths
|
|
243
247
|
.map((file) => file.path)
|
|
244
|
-
.filter((file) => /\.(svg|html|png|webp|gif|mp4|webm|jpe?g)
|
|
248
|
+
.filter((file) => /\.(svg|html|png|webp|gif|mp4|webm|jpe?g|pdf|dxf)$/i.test(file));
|
|
245
249
|
const { figures, errors: figErrors } = figuresCache.get(dirSig(figuresDir) +
|
|
246
250
|
visualFiles.map((file) => `${file}:${fileSig(textfilePath(root, file))}`).join("|"), () => loadFigures(figuresDir, visualFiles));
|
|
247
251
|
const siteErrors = figErrors.filter((error) => error.startsWith("figures/"));
|
|
252
|
+
if (manifest.licence) {
|
|
253
|
+
try {
|
|
254
|
+
assertNotIgnored(root, manifest.licence);
|
|
255
|
+
if (!statSync(textfilePath(root, manifest.licence)).isFile())
|
|
256
|
+
throw new Error("expected a regular file");
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
siteErrors.push(`${MANIFEST_FILE}: system.licence ${manifest.licence}: ${error instanceof Error ? error.message : error}`);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
248
262
|
if (!isLangTag(defaultLang)) {
|
|
249
263
|
siteErrors.push("defaultLang must be a BCP 47 language tag");
|
|
250
264
|
}
|
|
251
265
|
const previews = parsePreviewSettings(options.previews ?? defaultPreviewSettings());
|
|
252
266
|
let title;
|
|
253
267
|
let icon;
|
|
268
|
+
let iconVariants;
|
|
254
269
|
let iconFile;
|
|
255
270
|
const files = [];
|
|
256
|
-
if (!existsSync(contentDir) &&
|
|
271
|
+
if (!existsSync(contentDir) &&
|
|
272
|
+
!manifest.files &&
|
|
273
|
+
!flattenTree(manifest.tree).some((node) => node.subformat)) {
|
|
257
274
|
siteErrors.push(`${contentDir}: content/ is missing`);
|
|
258
275
|
}
|
|
259
276
|
else {
|
|
@@ -263,13 +280,23 @@ export const createCompiler = (root, options = {}) => {
|
|
|
263
280
|
docCache = createFileCache();
|
|
264
281
|
previewKey = nextPreviewKey;
|
|
265
282
|
}
|
|
266
|
-
title =
|
|
283
|
+
title =
|
|
284
|
+
manifest.titles?.[options.locale ?? contentLanguageCode(defaultLang)] ??
|
|
285
|
+
manifestTitle;
|
|
267
286
|
iconFile = manifestIcon;
|
|
268
287
|
if (iconFile) {
|
|
269
288
|
try {
|
|
270
289
|
icon = squareSvg(readTextfile(root, iconFile));
|
|
271
290
|
if (!icon)
|
|
272
291
|
throw new Error("expected an SVG with a viewBox or width and height");
|
|
292
|
+
const variants = Object.entries(manifest.themeIcons ?? themeVariants(root, iconFile, themes));
|
|
293
|
+
if (variants.length)
|
|
294
|
+
iconVariants = Object.fromEntries(variants.map(([theme, file]) => {
|
|
295
|
+
const svg = squareSvg(readTextfile(root, file));
|
|
296
|
+
if (!svg)
|
|
297
|
+
throw new Error(`${file}: expected an SVG with a viewBox or width and height`);
|
|
298
|
+
return [theme, svg];
|
|
299
|
+
}));
|
|
273
300
|
}
|
|
274
301
|
catch (error) {
|
|
275
302
|
siteErrors.push(`${MANIFEST_FILE}: icon ${iconFile}: ${error instanceof Error ? error.message : error}`);
|
|
@@ -278,9 +305,13 @@ export const createCompiler = (root, options = {}) => {
|
|
|
278
305
|
siteErrors.push(...manifestErrors);
|
|
279
306
|
// Directly under content/: flat <id>.md files only (no folders).
|
|
280
307
|
const onDisk = new Set();
|
|
281
|
-
|
|
308
|
+
const contentEntries = existsSync(contentDir)
|
|
282
309
|
? readdirSync(contentDir, { withFileTypes: true })
|
|
283
|
-
: []
|
|
310
|
+
: [];
|
|
311
|
+
const ignoredContent = ignoredFiles(root, contentEntries.map((entry) => `content/${entry.name}`));
|
|
312
|
+
for (const entry of contentEntries) {
|
|
313
|
+
if (ignoredContent.has(`content/${entry.name}`))
|
|
314
|
+
continue;
|
|
284
315
|
if (entry.name.startsWith(".")) {
|
|
285
316
|
continue;
|
|
286
317
|
}
|
|
@@ -294,7 +325,9 @@ export const createCompiler = (root, options = {}) => {
|
|
|
294
325
|
onDisk.add(id);
|
|
295
326
|
}
|
|
296
327
|
// Manifest pre-order is the display order.
|
|
297
|
-
for (const { id, parent } of flattenTree(tree)) {
|
|
328
|
+
for (const { id, parent, subformat } of flattenTree(tree)) {
|
|
329
|
+
if (subformat)
|
|
330
|
+
continue;
|
|
298
331
|
const rel = docFile(id, manifest.files);
|
|
299
332
|
if (rel.startsWith("content/")) {
|
|
300
333
|
const diskId = docIdOfFile(rel.slice("content/".length));
|
|
@@ -304,6 +337,7 @@ export const createCompiler = (root, options = {}) => {
|
|
|
304
337
|
let path;
|
|
305
338
|
try {
|
|
306
339
|
path = textfilePath(root, rel);
|
|
340
|
+
assertNotIgnored(root, rel);
|
|
307
341
|
}
|
|
308
342
|
catch (error) {
|
|
309
343
|
siteErrors.push(`${rel}: ${error}`);
|
|
@@ -320,6 +354,7 @@ export const createCompiler = (root, options = {}) => {
|
|
|
320
354
|
try {
|
|
321
355
|
const translated = textfilePath(root, file);
|
|
322
356
|
if (existsSync(translated)) {
|
|
357
|
+
assertNotIgnored(root, file);
|
|
323
358
|
parsed = docCache.get(translated, (raw) => parseFile(raw, previews, rel));
|
|
324
359
|
language = options.locale;
|
|
325
360
|
}
|
|
@@ -339,7 +374,12 @@ export const createCompiler = (root, options = {}) => {
|
|
|
339
374
|
if (!file.startsWith("figures/"))
|
|
340
375
|
siteErrors.push(...figErrors.filter((error) => error.startsWith(`${file}:`)));
|
|
341
376
|
// Icons are small, so read them every time (no cache).
|
|
342
|
-
const { icons, errors: iconErrors } = loadIcons(root, new Set(
|
|
377
|
+
const { icons, errors: iconErrors } = loadIcons(root, new Set([
|
|
378
|
+
...files.map((f) => f.doc.id),
|
|
379
|
+
...flattenTree(manifest.tree)
|
|
380
|
+
.filter((node) => node.subformat)
|
|
381
|
+
.map((node) => node.id),
|
|
382
|
+
]), iconFile, manifest.files, Object.values(manifest.themeIcons ?? {}));
|
|
343
383
|
siteErrors.push(...iconErrors);
|
|
344
384
|
for (const { doc } of files) {
|
|
345
385
|
const icon = icons.get(doc.id);
|
|
@@ -348,9 +388,6 @@ export const createCompiler = (root, options = {}) => {
|
|
|
348
388
|
}
|
|
349
389
|
}
|
|
350
390
|
const docs = files.map((f) => f.doc);
|
|
351
|
-
if (docs.length === 0) {
|
|
352
|
-
siteErrors.push(`${contentDir}: no docs`);
|
|
353
|
-
}
|
|
354
391
|
const ctx = {
|
|
355
392
|
docs,
|
|
356
393
|
figures: new Map(figures.map((f) => [f.name, f])),
|
|
@@ -363,18 +400,85 @@ export const createCompiler = (root, options = {}) => {
|
|
|
363
400
|
errors.push(...errs.map((e) => `${doc.file}: ${e}`));
|
|
364
401
|
sources.set(doc.file, {
|
|
365
402
|
terms: parsed.parsed.terms,
|
|
403
|
+
referencedFiles: parsed.referencedFiles,
|
|
366
404
|
raw: parsed.raw,
|
|
367
405
|
frontmatter: parsed.parsed.frontmatter,
|
|
368
406
|
lines: parsed.lines,
|
|
369
407
|
errors: errs,
|
|
370
408
|
});
|
|
371
409
|
}
|
|
410
|
+
for (const node of flattenTree(manifest.tree).filter((node) => node.subformat)) {
|
|
411
|
+
const config = manifest.subformats?.find((format) => format.ext === node.subformat);
|
|
412
|
+
if (!config)
|
|
413
|
+
continue;
|
|
414
|
+
const file = `subformat:${config.ext}`;
|
|
415
|
+
const title = node.title ?? config.ext;
|
|
416
|
+
const pageErrors = [];
|
|
417
|
+
const collection = {
|
|
418
|
+
...config,
|
|
419
|
+
fields: [],
|
|
420
|
+
records: [],
|
|
421
|
+
};
|
|
422
|
+
try {
|
|
423
|
+
assertNotIgnored(root, config.schema);
|
|
424
|
+
if (!paths.some((file) => file.path === config.schema))
|
|
425
|
+
throw new Error("Subformat schema is not available in the workspace");
|
|
426
|
+
const loaded = loadSubformat(root, config, previews.domains);
|
|
427
|
+
collection.fields = loaded.fields;
|
|
428
|
+
collection.template = loaded.template;
|
|
429
|
+
collection.defaultFolder = manifest.defaultSubformatFolder;
|
|
430
|
+
for (const record of paths.filter((file) => file.extension === config.ext && file.path !== config.schema)) {
|
|
431
|
+
try {
|
|
432
|
+
const content = readTextfile(root, record.path);
|
|
433
|
+
const data = parseSubformatRecord(content, config.format);
|
|
434
|
+
validateSubformatRecord(loaded, data);
|
|
435
|
+
collection.records.push({ file: record.path, content, data });
|
|
436
|
+
}
|
|
437
|
+
catch (error) {
|
|
438
|
+
pageErrors.push(`${record.path}: ${error instanceof Error ? error.message : error}`);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
catch (error) {
|
|
443
|
+
pageErrors.push(`${config.schema}: ${error instanceof Error ? error.message : error}`);
|
|
444
|
+
}
|
|
445
|
+
docs.push({
|
|
446
|
+
id: node.id,
|
|
447
|
+
parent: node.parent,
|
|
448
|
+
title,
|
|
449
|
+
file,
|
|
450
|
+
lang: defaultLang,
|
|
451
|
+
subformat: collection,
|
|
452
|
+
icon: icons.get(node.id),
|
|
453
|
+
body: "",
|
|
454
|
+
blocks: [],
|
|
455
|
+
toc: [],
|
|
456
|
+
refs: { figures: [], terms: [] },
|
|
457
|
+
});
|
|
458
|
+
sources.set(file, {
|
|
459
|
+
terms: [],
|
|
460
|
+
referencedFiles: [],
|
|
461
|
+
raw: "",
|
|
462
|
+
frontmatter: { title },
|
|
463
|
+
lines: [],
|
|
464
|
+
errors: pageErrors,
|
|
465
|
+
});
|
|
466
|
+
errors.push(...pageErrors);
|
|
467
|
+
}
|
|
468
|
+
const order = flattenTree(manifest.tree).map((node) => node.id);
|
|
469
|
+
docs.sort((a, b) => order.indexOf(a.id) - order.indexOf(b.id));
|
|
470
|
+
if (!docs.length) {
|
|
471
|
+
siteErrors.push(`${contentDir}: no docs`);
|
|
472
|
+
errors.push(`${contentDir}: no docs`);
|
|
473
|
+
}
|
|
372
474
|
return {
|
|
373
475
|
previews,
|
|
476
|
+
subformats: manifest.subformats,
|
|
374
477
|
...(manifest.languages ? { contentLanguages: manifest.languages } : {}),
|
|
375
478
|
...(manifest.themes ? { themes: manifest.themes } : {}),
|
|
376
479
|
...(title === undefined ? {} : { title }),
|
|
377
480
|
...(icon === undefined ? {} : { icon }),
|
|
481
|
+
...(iconVariants === undefined ? {} : { iconVariants }),
|
|
378
482
|
docs,
|
|
379
483
|
terms: [...termMap(files.flatMap(({ parsed }) => parsed.parsed.terms)).values()],
|
|
380
484
|
figures,
|
|
@@ -399,6 +503,7 @@ export const compileDraft = (ctx, file, raw) => {
|
|
|
399
503
|
doc: toDoc(known?.id ?? "", known?.parent, file, parsed, ctx.defaultLang ?? DEFAULT_LANG),
|
|
400
504
|
source: {
|
|
401
505
|
terms: parsed.parsed.terms,
|
|
506
|
+
referencedFiles: parsed.referencedFiles,
|
|
402
507
|
raw,
|
|
403
508
|
frontmatter: parsed.parsed.frontmatter,
|
|
404
509
|
lines: parsed.lines,
|
package/dist/compiler/config.js
CHANGED
|
@@ -6,6 +6,7 @@ import { existsSync } from "node:fs";
|
|
|
6
6
|
import { join } from "node:path";
|
|
7
7
|
import { pathToFileURL } from "node:url";
|
|
8
8
|
import { isLangTag } from "../core/language.js";
|
|
9
|
+
import { ignoredFiles } from "./ignored-files.js";
|
|
9
10
|
export const CONFIG_FILE = "docs.config.mjs";
|
|
10
11
|
const FUNCTION_KEYS = ["isTermBoundary", "compareTerms", "lint"];
|
|
11
12
|
const checkAdapter = (value, i) => {
|
|
@@ -30,7 +31,7 @@ const checkAdapter = (value, i) => {
|
|
|
30
31
|
/** Reads docs.config.mjs; defaults when absent. Throws with a readable message when invalid. */
|
|
31
32
|
export const loadDocsConfig = async (root) => {
|
|
32
33
|
const file = join(root, CONFIG_FILE);
|
|
33
|
-
if (!existsSync(file)) {
|
|
34
|
+
if (!existsSync(file) || ignoredFiles(root, [CONFIG_FILE]).has(CONFIG_FILE)) {
|
|
34
35
|
return {
|
|
35
36
|
languages: [],
|
|
36
37
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const dxfSvg: (source: string) => string;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { runInNewContext } from "node:vm";
|
|
2
|
+
import { Helper } from "dxf";
|
|
3
|
+
import { escapeHtml } from "../core/escape-html.js";
|
|
4
|
+
export const dxfSvg = (source) => {
|
|
5
|
+
source = source.replace(/\r\n?/g, "\n").trim();
|
|
6
|
+
if (source.includes("\0") ||
|
|
7
|
+
!/^\s*0\s*\nSECTION\s*$/m.test(source) ||
|
|
8
|
+
!/\n\s*0\s*\nEOF$/.test(source))
|
|
9
|
+
throw new Error("Expected an ASCII DXF file");
|
|
10
|
+
// Bound conversion time so malformed geometry cannot stall the synchronous compiler.
|
|
11
|
+
const output = runInNewContext("render()", {
|
|
12
|
+
render: () => {
|
|
13
|
+
const helper = new Helper(source);
|
|
14
|
+
return {
|
|
15
|
+
svg: helper.toSVG(),
|
|
16
|
+
texts: helper.parsed?.entities.filter((entity) => entity.type === "TEXT") ?? [],
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
}, { timeout: 2000 });
|
|
20
|
+
const svg = output.svg.replace(/^<\?xml[^>]*>\s*/, "");
|
|
21
|
+
const bounds = /viewBox="([^"]+)"/.exec(svg)?.[1]?.split(/\s+/).map(Number);
|
|
22
|
+
if (!bounds ||
|
|
23
|
+
bounds.length !== 4 ||
|
|
24
|
+
!bounds.every(Number.isFinite) ||
|
|
25
|
+
/NaN|Infinity/.test(svg))
|
|
26
|
+
throw new Error("DXF contains no supported 2D geometry");
|
|
27
|
+
const [x = 0, y = 0, width = 0, height = 0] = bounds;
|
|
28
|
+
const geometry = /<(?:path|circle|ellipse)\b/.test(svg);
|
|
29
|
+
let minX = geometry ? x : Number.POSITIVE_INFINITY;
|
|
30
|
+
let minY = geometry ? y : Number.POSITIVE_INFINITY;
|
|
31
|
+
let maxX = geometry ? x + width : Number.NEGATIVE_INFINITY;
|
|
32
|
+
let maxY = geometry ? y + height : Number.NEGATIVE_INFINITY;
|
|
33
|
+
const labels = [];
|
|
34
|
+
// shortcut: top-level TEXT uses a generic font; use a CAD renderer for block text, MTEXT and native dimensions.
|
|
35
|
+
for (const text of output.texts) {
|
|
36
|
+
if (!text.string || text.visible === false)
|
|
37
|
+
continue;
|
|
38
|
+
const aligned = text.hAlign || text.vAlign;
|
|
39
|
+
const tx = (aligned ? text.x2 : text.x) ?? text.x ?? 0;
|
|
40
|
+
const ty = -((aligned ? text.y2 : text.y) ?? text.y ?? 0);
|
|
41
|
+
const size = text.textHeight ?? 2.5;
|
|
42
|
+
const rotation = -(text.rotation ?? 0);
|
|
43
|
+
const scale = text.relScaleX ?? 1;
|
|
44
|
+
if (![tx, ty, size, rotation, scale].every(Number.isFinite) || size <= 0 || scale <= 0)
|
|
45
|
+
throw new Error("DXF text requires finite coordinates and a positive height and width scale");
|
|
46
|
+
const value = text.string.replace(/%%c/gi, "⌀").replace(/%%d/gi, "°").replace(/%%p/gi, "±");
|
|
47
|
+
const anchor = text.hAlign === 1 ? "middle" : text.hAlign === 2 ? "end" : "start";
|
|
48
|
+
const baseline = ["auto", "text-after-edge", "central", "text-before-edge"][text.vAlign ?? 0] ?? "auto";
|
|
49
|
+
const span = value.length * size * scale;
|
|
50
|
+
const left = anchor === "middle" ? -span / 2 : anchor === "end" ? -span : 0;
|
|
51
|
+
const angle = (rotation / 180) * Math.PI;
|
|
52
|
+
for (const dx of [left, left + span]) {
|
|
53
|
+
for (const dy of [-size, size]) {
|
|
54
|
+
const px = tx + dx * Math.cos(angle) - dy * Math.sin(angle);
|
|
55
|
+
const py = ty + dx * Math.sin(angle) + dy * Math.cos(angle);
|
|
56
|
+
minX = Math.min(minX, px);
|
|
57
|
+
minY = Math.min(minY, py);
|
|
58
|
+
maxX = Math.max(maxX, px);
|
|
59
|
+
maxY = Math.max(maxY, py);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
labels.push(`<text transform="translate(${tx} ${ty}) rotate(${rotation}) scale(${scale} 1)" font-family="sans-serif" font-size="${size}" text-anchor="${anchor}" dominant-baseline="${baseline}" fill="var(--doc-fg, #000000)">${escapeHtml(value)}</text>`);
|
|
63
|
+
}
|
|
64
|
+
if (!geometry && !labels.length)
|
|
65
|
+
throw new Error("DXF contains no supported 2D geometry or text");
|
|
66
|
+
const padding = Math.max(maxX - minX, maxY - minY, 1) * 0.04;
|
|
67
|
+
const viewBox = [
|
|
68
|
+
minX - padding,
|
|
69
|
+
minY - padding,
|
|
70
|
+
maxX - minX + padding * 2,
|
|
71
|
+
maxY - minY + padding * 2,
|
|
72
|
+
];
|
|
73
|
+
if (!viewBox.every(Number.isFinite))
|
|
74
|
+
throw new Error("DXF text exceeds finite preview bounds");
|
|
75
|
+
return svg
|
|
76
|
+
.replace(/viewBox="[^"]+"/, `viewBox="${viewBox.join(" ")}"`)
|
|
77
|
+
.replace('width="100%"', 'width="800"')
|
|
78
|
+
.replace(/<(path|circle|ellipse)\b/g, '<$1 vector-effect="non-scaling-stroke" stroke-width="1.25"')
|
|
79
|
+
.replace(/stroke="(?:#000000|rgb\(0, 0, 0\))"/g, 'stroke="var(--doc-fg, #000000)"')
|
|
80
|
+
.replace("</svg>", () => `${labels.join("")}</svg>`);
|
|
81
|
+
};
|