@hia-doc/html-doc-source-map 0.1.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 (4) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +21 -0
  3. package/package.json +21 -0
  4. package/src/index.mjs +157 -0
package/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ +MIT License
2
+
3
+ Copyright (c) 2026 HIA
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,21 @@
1
+ # HTML Doc Source Map
2
+
3
+ Creates safe documentation source-map references and direct-source manifests
4
+ for HTMDoc extraction artifacts.
5
+
6
+ ```sh
7
+ npm install @hia-doc/html-doc-source-map
8
+ ```
9
+
10
+ ```js
11
+ import { createHtmlDocumentationSourceMap } from "@hia-doc/html-doc-source-map";
12
+
13
+ const manifest = createHtmlDocumentationSourceMap({
14
+ extraction,
15
+ extractionPath: "artifacts/card.htmdoc.json",
16
+ hiaDocumentPath: "artifacts/card.hia.json"
17
+ });
18
+ ```
19
+
20
+ Source paths are workspace-relative, artifact paths are output-relative and
21
+ source content is not embedded by default.
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@hia-doc/html-doc-source-map",
3
+ "version": "0.1.0",
4
+ "description": "HTML documentation source-map linkage.",
5
+ "license": "MIT",
6
+ "keywords": ["documentation", "html", "source-map", "htmdoc"],
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/mandolin/hia-htmdoc.git",
10
+ "directory": "packages/html-doc-source-map"
11
+ },
12
+ "bugs": { "url": "https://github.com/mandolin/hia-htmdoc/issues" },
13
+ "homepage": "https://github.com/mandolin/hia-htmdoc/tree/main/packages/html-doc-source-map#readme",
14
+ "type": "module",
15
+ "exports": {
16
+ ".": "./src/index.mjs"
17
+ },
18
+ "files": ["src", "README.md", "LICENSE"],
19
+ "engines": { "node": ">=20.19.0" },
20
+ "publishConfig": { "access": "public" }
21
+ }
package/src/index.mjs ADDED
@@ -0,0 +1,157 @@
1
+ export const HTML_DOC_SOURCE_CONTENT_POLICIES = Object.freeze(["none", "reference", "embed"]);
2
+
3
+ // <lang><zh-CN>doc-source-map provenance 使用公开候选版本,同时不改变 draft contractVersion。</zh-CN><en>Doc-source-map provenance uses the public candidate version without changing the draft contractVersion.</en></lang>
4
+ const HTML_DOC_SOURCE_MAP_VERSION = "0.1.0";
5
+
6
+ export function createHtmlDocSourceMapRef(options = {}) {
7
+ return {
8
+ kind: "hia-doc-source-map-ref",
9
+ href: options.href ?? null,
10
+ sourcesContentPolicy: normalizeSourcesContentPolicy(options.sourcesContentPolicy ?? "none")
11
+ };
12
+ }
13
+
14
+ export function normalizeSourcesContentPolicy(policy) {
15
+ if (!HTML_DOC_SOURCE_CONTENT_POLICIES.includes(policy)) {
16
+ throw new Error(`Unsupported HTMDoc sourcesContent policy: ${policy}`);
17
+ }
18
+ return policy;
19
+ }
20
+
21
+ export function createHtmlDocumentationSourceMap(options = {}) {
22
+ const extraction = options.extraction;
23
+ if (!extraction || extraction.contract !== "hia-htmdoc-extraction") {
24
+ throw new Error("Expected hia-htmdoc-extraction artifact.");
25
+ }
26
+
27
+ const extractionPath = normalizeSafePath(options.extractionPath ?? "document.htmdoc.json");
28
+ const hiaDocumentPath = normalizeSafePath(options.hiaDocumentPath ?? "document.hia.json");
29
+ const sourcePath = normalizeSafePath(extraction.source?.path ?? "input.html");
30
+ const sourceLanguage = extraction.source?.kind === "custom-elements-manifest" ? "json" : "html";
31
+ const sourceId = `source:${sourceLanguage}:${slug(sourcePath)}`;
32
+ const extractionArtifactId = "artifact:htmdoc:extraction";
33
+ const hiaArtifactId = "artifact:hia:document";
34
+ const entryIds = extraction.symbols.map((symbol) => `entry:${slug(symbol.id)}`);
35
+ const sourcesContentPolicy = normalizeSourcesContentPolicy(
36
+ options.sourcesContentPolicy ?? extraction.sourceMap?.sourcesContentPolicy ?? "none"
37
+ );
38
+
39
+ return {
40
+ contract: "doc-source-map",
41
+ contractVersion: "0.1.0-draft",
42
+ id: options.id ?? `docmap:htmdoc:${slug(sourcePath)}`,
43
+ producer: {
44
+ name: "@hia-doc/html-doc-source-map",
45
+ version: HTML_DOC_SOURCE_MAP_VERSION,
46
+ runtime: "node",
47
+ profile: "htmdoc-direct-source"
48
+ },
49
+ pathBases: {
50
+ artifacts: "outputDirectory",
51
+ sources: "workspaceRoot"
52
+ },
53
+ artifacts: [
54
+ {
55
+ id: extractionArtifactId,
56
+ kind: "extraction-artifact",
57
+ path: extractionPath,
58
+ language: "json",
59
+ role: "generated",
60
+ contractRefs: [{
61
+ contract: extraction.contract,
62
+ contractVersion: extraction.contractVersion,
63
+ path: extractionPath
64
+ }]
65
+ },
66
+ {
67
+ id: hiaArtifactId,
68
+ kind: "hia-document",
69
+ path: hiaDocumentPath,
70
+ language: "json",
71
+ role: "generated"
72
+ }
73
+ ],
74
+ sources: [{
75
+ id: sourceId,
76
+ kind: extraction.source?.kind ?? "html",
77
+ path: sourcePath,
78
+ language: sourceLanguage,
79
+ role: "original",
80
+ sourcesContentPolicy
81
+ }],
82
+ sourceMaps: [],
83
+ chains: [{
84
+ id: `chain:htmdoc:${slug(sourcePath)}`,
85
+ stages: [
86
+ {
87
+ from: sourceId,
88
+ to: extractionArtifactId,
89
+ transform: extraction.source?.kind === "custom-elements-manifest" ? "cem-to-htmdoc" : "html-to-htmdoc",
90
+ sourceMap: null,
91
+ linkage: entryIds
92
+ },
93
+ {
94
+ from: extractionArtifactId,
95
+ to: hiaArtifactId,
96
+ transform: "htmdoc-to-hia-core",
97
+ sourceMap: null,
98
+ linkage: entryIds
99
+ }
100
+ ]
101
+ }],
102
+ entries: extraction.symbols.map((symbol, index) => ({
103
+ id: entryIds[index],
104
+ kind: "symbol",
105
+ symbolId: symbol.id,
106
+ symbolKind: symbol.kind,
107
+ ...(symbol.metadata?.tag
108
+ ? { annotation: { tag: symbol.metadata.tag, value: symbol.metadata.value ?? "" } }
109
+ : {}),
110
+ sourceRefs: [{
111
+ sourceId,
112
+ range: symbol.source?.range ?? undefined,
113
+ rangeSource: extraction.source?.kind === "custom-elements-manifest" ? "adapter" : "parser",
114
+ confidence: symbol.source?.range ? "high" : "medium"
115
+ }],
116
+ artifactRefs: [
117
+ {
118
+ artifactId: extractionArtifactId,
119
+ selector: `/symbols/${index}`,
120
+ rangeSource: "adapter",
121
+ confidence: "high"
122
+ },
123
+ {
124
+ artifactId: hiaArtifactId,
125
+ selector: `/symbols/${index}`,
126
+ rangeSource: "adapter",
127
+ confidence: "high"
128
+ }
129
+ ],
130
+ diagnostics: []
131
+ })),
132
+ privacy: {
133
+ sourcesContentPolicy,
134
+ allowAbsolutePaths: false,
135
+ allowUncPaths: false,
136
+ allowPathTraversal: false,
137
+ releaseGate: {
138
+ requireExplicitEmbedOptIn: true,
139
+ failOnUnsafePath: true,
140
+ failOnUnexpectedSourcesContent: true
141
+ }
142
+ },
143
+ diagnostics: []
144
+ };
145
+ }
146
+
147
+ function normalizeSafePath(value) {
148
+ const normalized = String(value).replaceAll("\\", "/");
149
+ if (!normalized || normalized.startsWith("/") || /^[A-Za-z][A-Za-z0-9+.-]*:/.test(normalized) || normalized.split("/").includes("..")) {
150
+ throw new Error(`Unsafe doc-source-map path: ${value}`);
151
+ }
152
+ return normalized;
153
+ }
154
+
155
+ function slug(value) {
156
+ return String(value).trim().toLowerCase().replace(/[^a-z0-9_-]+/g, "-").replace(/^-|-$/g, "") || "unnamed";
157
+ }