igniteui-astro-components 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/LICENSE +21 -0
- package/README.md +374 -0
- package/package.json +109 -0
- package/src/components/DocsSidebar/DocsSidebar.astro +99 -0
- package/src/components/DocsSidebar/README.md +166 -0
- package/src/components/DocsSidebar/SidebarFilterInput.astro +74 -0
- package/src/components/DocsSidebar/SidebarLabel.astro +40 -0
- package/src/components/DocsSidebar/sidebar-filter.ts +393 -0
- package/src/components/DocsSidebar/toTreeNodes.ts +66 -0
- package/src/components/DocsSubHeader/DocsSubHeader.astro +320 -0
- package/src/components/DocsSubHeader/README.md +74 -0
- package/src/components/DocsToc/DocsToc.astro +307 -0
- package/src/components/DocsToc/README.md +68 -0
- package/src/components/DocsToc/toTreeNodes.ts +57 -0
- package/src/components/DocsTree/DocsTree.astro +66 -0
- package/src/components/DocsTree/DocsTreeItem.astro +84 -0
- package/src/components/DocsTree/README.md +105 -0
- package/src/components/DocsTree/docs-tree.css +227 -0
- package/src/components/DocsTree/types.ts +47 -0
- package/src/components/GlobalFooter/GlobalFooter.astro +90 -0
- package/src/components/GlobalFooter/GlobalFooter.module.scss +23 -0
- package/src/components/GlobalFooter/README.md +35 -0
- package/src/components/GlobalNavBar/GlobalNavBar.astro +218 -0
- package/src/components/GlobalNavBar/GlobalNavBar.module.scss +215 -0
- package/src/components/GlobalNavBar/README.md +49 -0
- package/src/components/MobileSidebarToggle/MobileSidebarToggle.astro +112 -0
- package/src/components/MobileSidebarToggle/README.md +31 -0
- package/src/components/Search/README.md +43 -0
- package/src/components/Search/Search.astro +222 -0
- package/src/components/ThemingWidget/README.md +35 -0
- package/src/components/ThemingWidget/ThemingWidget.astro +152 -0
- package/src/components/mdx/ApiLink/ApiLink.astro +121 -0
- package/src/components/mdx/ApiLink/README.md +68 -0
- package/src/components/mdx/ApiRef/ApiRef.astro +92 -0
- package/src/components/mdx/ApiRef/README.md +50 -0
- package/src/components/mdx/DocsAside/DocsAside.astro +86 -0
- package/src/components/mdx/DocsAside/README.md +66 -0
- package/src/components/mdx/DocsAside/icons/caution.svg +3 -0
- package/src/components/mdx/DocsAside/icons/danger.svg +3 -0
- package/src/components/mdx/DocsAside/icons/note.svg +3 -0
- package/src/components/mdx/DocsAside/icons/tip.svg +4 -0
- package/src/components/mdx/PlatformBlock/PlatformBlock.astro +24 -0
- package/src/components/mdx/PlatformBlock/README.md +57 -0
- package/src/components/mdx/Sample/README.md +59 -0
- package/src/components/mdx/Sample/Sample.astro +142 -0
- package/src/content-helper.ts +159 -0
- package/src/content.config.ts +17 -0
- package/src/index.ts +42 -0
- package/src/integration.ts +841 -0
- package/src/layouts/DocsLayout/DocsLayout.astro +307 -0
- package/src/layouts/DocsLayout/README.md +124 -0
- package/src/lib/nav-helpers.ts +155 -0
- package/src/lib/platform-context.ts +305 -0
- package/src/lib/sidebar/helpers.ts +81 -0
- package/src/lib/sidebar/types.ts +36 -0
- package/src/llms.ts +315 -0
- package/src/platform.ts +281 -0
- package/src/plugins/remark-docfx.ts +368 -0
- package/src/scripts/sample-widget.ts +516 -0
- package/src/sidebar.ts +167 -0
- package/src/styles/custom.scss +254 -0
- package/src/styles/ig-theme.scss +43 -0
- package/src/virtual-modules.d.ts +35 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remark plugin that transforms docfx-specific markdown syntax into
|
|
3
|
+
* standard HTML / markdown that Starlight can render.
|
|
4
|
+
*
|
|
5
|
+
* Handles:
|
|
6
|
+
* 1. {environment:...} variable substitution in text, links, and raw HTML
|
|
7
|
+
* 2. <code-view> elements -> .ig-code-view placeholder divs (enhanced by code-view.js)
|
|
8
|
+
* 3. <div class="divider--half"></div> -> <hr>
|
|
9
|
+
* 4. Relative .md link rewriting for Astro trailing-slash URLs
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { visit } from 'unist-util-visit';
|
|
13
|
+
import fs from 'node:fs';
|
|
14
|
+
import path from 'node:path';
|
|
15
|
+
|
|
16
|
+
// ENV is loaded lazily the first time replaceEnvVars() is called — NOT at
|
|
17
|
+
// module load time. Module-level code runs while the Astro config is being
|
|
18
|
+
// evaluated, before createDocsSite() has had a chance to set DOCS_SOURCE_PATH.
|
|
19
|
+
// Deferring the load ensures we read the correct environment.json for the
|
|
20
|
+
// active project (Angular, Blazor, React, WC, …).
|
|
21
|
+
let _ENV: Record<string, string> | null = null;
|
|
22
|
+
let _envSourcePath: string | null = null; // tracks which DOCS_SOURCE_PATH was used
|
|
23
|
+
|
|
24
|
+
function loadEnv(): Record<string, string> {
|
|
25
|
+
const currentPath = process.env.DOCS_SOURCE_PATH ?? null;
|
|
26
|
+
// Re-load if the source path changed (e.g. two projects built in the same process)
|
|
27
|
+
if (_ENV !== null && currentPath === _envSourcePath) return _ENV as Record<string, string>;
|
|
28
|
+
_envSourcePath = currentPath;
|
|
29
|
+
_ENV = null;
|
|
30
|
+
|
|
31
|
+
if (!process.env.DOCS_SOURCE_PATH) { _ENV = {}; return _ENV as Record<string, string>; }
|
|
32
|
+
const sourceRoot = path.resolve(process.env.DOCS_SOURCE_PATH);
|
|
33
|
+
|
|
34
|
+
// Search order:
|
|
35
|
+
// 1. {docsDir}/en/environment.json — docfx repo layout
|
|
36
|
+
// 2. {docsDir}/environment.json — flat layout
|
|
37
|
+
// 3. {docsDir}/../environment.json — xplat layout: docsDir = dist/{P}/{lang}/components,
|
|
38
|
+
// environment.json lives one level up
|
|
39
|
+
// 4. {docsDir}/../en/environment.json
|
|
40
|
+
const parent = path.dirname(sourceRoot);
|
|
41
|
+
const candidates = [
|
|
42
|
+
path.join(sourceRoot, 'en', 'environment.json'),
|
|
43
|
+
path.join(sourceRoot, 'environment.json'),
|
|
44
|
+
path.join(parent, 'environment.json'),
|
|
45
|
+
path.join(parent, 'en', 'environment.json'),
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
const envPath = candidates.find(c => fs.existsSync(c));
|
|
49
|
+
if (!envPath) { _ENV = {}; return _ENV; }
|
|
50
|
+
|
|
51
|
+
try {
|
|
52
|
+
const envData = JSON.parse(fs.readFileSync(envPath, 'utf-8'));
|
|
53
|
+
// DOCS_ENV overrides explicitly (useful for staging builds).
|
|
54
|
+
// Otherwise fall back to NODE_ENV ('development' | 'production').
|
|
55
|
+
// Matches environment.json keys: development / staging / production.
|
|
56
|
+
const envKey = process.env.DOCS_ENV ?? process.env.NODE_ENV ?? 'production';
|
|
57
|
+
_ENV = envData[envKey] ?? envData.production ?? {};
|
|
58
|
+
} catch {
|
|
59
|
+
_ENV = {};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
return _ENV as Record<string, string>;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const ENV_PATTERN = /\{environment:(\w+)\}/g;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Rewrite a relative .md link to a root-relative Astro URL.
|
|
69
|
+
* Absolute, root-relative, fragment-only, and protocol links are left untouched.
|
|
70
|
+
* @param url - Raw link URL from the markdown AST.
|
|
71
|
+
* @param filePath - Absolute path of the current .md file.
|
|
72
|
+
* @param docsDir - Absolute path to the docs root (DOCS_SOURCE_PATH).
|
|
73
|
+
*/
|
|
74
|
+
function rewriteMdLink(url: string, filePath: string, docsDir: string): string {
|
|
75
|
+
if (!url) return url;
|
|
76
|
+
if (
|
|
77
|
+
url.startsWith('http://') || url.startsWith('https://') ||
|
|
78
|
+
url.startsWith('/') || url.startsWith('#') || url.startsWith('mailto:')
|
|
79
|
+
) return url;
|
|
80
|
+
|
|
81
|
+
// Separate path from fragment / query-string suffix.
|
|
82
|
+
const hashIdx = url.indexOf('#');
|
|
83
|
+
const qIdx = url.indexOf('?');
|
|
84
|
+
const splitAt = hashIdx !== -1 ? hashIdx : qIdx !== -1 ? qIdx : -1;
|
|
85
|
+
let mdPath = splitAt !== -1 ? url.slice(0, splitAt) : url;
|
|
86
|
+
const suffix = splitAt !== -1 ? url.slice(splitAt) : '';
|
|
87
|
+
|
|
88
|
+
if (!mdPath.endsWith('.md')) return url;
|
|
89
|
+
|
|
90
|
+
// Resolve the link relative to the current file's directory.
|
|
91
|
+
const fileDir = path.dirname(filePath);
|
|
92
|
+
const resolved = path.resolve(fileDir, mdPath);
|
|
93
|
+
|
|
94
|
+
// Compute the slug: path relative to docsDir, forward slashes, no .md extension.
|
|
95
|
+
const rel = path.relative(docsDir, resolved).replace(/\\/g, '/');
|
|
96
|
+
const slug = rel.endsWith('.md') ? rel.slice(0, -3) : rel;
|
|
97
|
+
|
|
98
|
+
// Return a URL with the site base prepended so links remain correct when
|
|
99
|
+
// the site is mounted at a sub-path (e.g. /docs-react-new/).
|
|
100
|
+
// DOCS_BASE is set by createDocsSite() in integration.ts; it is empty in dev mode.
|
|
101
|
+
const docsBase = (process.env.DOCS_BASE ?? '').replace(/\/$/, '');
|
|
102
|
+
return docsBase + '/' + slug.toLowerCase() + '/' + suffix;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function replaceEnvVars(str: string): string {
|
|
106
|
+
if (!str || typeof str !== 'string') return str;
|
|
107
|
+
const env = loadEnv();
|
|
108
|
+
return str.replace(ENV_PATTERN, (_match, key) => env[key] ?? `{environment:${key}}`);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Build the pre-rendered widget HTML shell (matches the Sample.astro structure).
|
|
113
|
+
* Picked up at runtime by sample-widget.ts via `.code-view[data-platform]`.
|
|
114
|
+
*/
|
|
115
|
+
function buildWidgetHtml(
|
|
116
|
+
src: string,
|
|
117
|
+
demosBase: string,
|
|
118
|
+
githubSrc: string,
|
|
119
|
+
height: string,
|
|
120
|
+
alt: string,
|
|
121
|
+
platform: string,
|
|
122
|
+
): string {
|
|
123
|
+
const widgetId = 'cw' + src.replace(/[^a-z0-9]/gi, '-');
|
|
124
|
+
const tabId = `${widgetId}-example`;
|
|
125
|
+
const safeAlt = alt.replace(/"/g, '"');
|
|
126
|
+
return (
|
|
127
|
+
`<div class="code-view" id="${widgetId}"` +
|
|
128
|
+
` data-iframe-src="${src}"` +
|
|
129
|
+
(demosBase ? ` data-demos-base-url="${demosBase}"` : '') +
|
|
130
|
+
(githubSrc ? ` data-github-src="${githubSrc}"` : '') +
|
|
131
|
+
` data-platform="${platform}">` +
|
|
132
|
+
`<div class="code-view-navbar">` +
|
|
133
|
+
`<div class="code-view-tab code-view-tab--active" data-tab-id="${tabId}">EXAMPLE</div>` +
|
|
134
|
+
`<span class="fs-button-container" title="Expand to fullscreen"></span>` +
|
|
135
|
+
`</div>` +
|
|
136
|
+
`<div class="code-views-container">` +
|
|
137
|
+
`<div id="${tabId}" class="sample-container code-view-tab-content loading" style="height: ${height}">` +
|
|
138
|
+
`<iframe data-src="${src}" title="${safeAlt}" style="width: 100%; height: 100%;" frameborder="0" seamless=""></iframe>` +
|
|
139
|
+
`</div>` +
|
|
140
|
+
`</div>` +
|
|
141
|
+
`</div>`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Transform <code-view ...> ... </code-view> raw HTML blocks into the
|
|
147
|
+
* pre-rendered widget shell consumed by sample-widget.ts at runtime.
|
|
148
|
+
*/
|
|
149
|
+
function transformCodeView(html: string): string {
|
|
150
|
+
return html.replace(
|
|
151
|
+
/<code-view\s+([\s\S]*?)>\s*(?:<\/code-view>)?/g,
|
|
152
|
+
(_match, attrs: string) => {
|
|
153
|
+
const stackblitzMatch = attrs.match(/stackblitz="([^"]*)"/);
|
|
154
|
+
const codesandboxMatch = attrs.match(/codesandbox="([^"]*)"/);
|
|
155
|
+
const srcMatch = attrs.match(/iframe-src="([^"]*)"/);
|
|
156
|
+
const demosBaseMatch = attrs.match(/data-demos-base-url="([^"]*)"/);
|
|
157
|
+
const githubSrcMatch = attrs.match(/github-src="([^"]*)"/);
|
|
158
|
+
const heightMatch = attrs.match(/style="height:\s*(\d+px)"/);
|
|
159
|
+
const altMatch = attrs.match(/alt="([^"]*)"/);
|
|
160
|
+
const height = heightMatch ? heightMatch[1] : '400px';
|
|
161
|
+
const alt = altMatch ? altMatch[1] : 'Demo';
|
|
162
|
+
|
|
163
|
+
// Explicit StackBlitz embed (stackblitz="<project-or-url>")
|
|
164
|
+
if (stackblitzMatch) {
|
|
165
|
+
const project = replaceEnvVars(stackblitzMatch[1]);
|
|
166
|
+
const embedUrl = project.startsWith('http')
|
|
167
|
+
? project
|
|
168
|
+
: `https://stackblitz.com/edit/${project}?embed=1&file=src/app/app.component.ts`;
|
|
169
|
+
return `<iframe src="${embedUrl}" style="width:100%;height:${height};border:1px solid #e5e7eb;border-radius:8px;" title="${alt} (StackBlitz)" loading="lazy" allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// Explicit CodeSandbox embed (codesandbox="<project-or-url>")
|
|
173
|
+
if (codesandboxMatch) {
|
|
174
|
+
const project = replaceEnvVars(codesandboxMatch[1]);
|
|
175
|
+
const embedUrl = project.startsWith('http')
|
|
176
|
+
? project
|
|
177
|
+
: `https://codesandbox.io/embed/${project}?fontsize=14&hidenavigation=1&theme=light`;
|
|
178
|
+
return `<iframe src="${embedUrl}" style="width:100%;height:${height};border:1px solid #e5e7eb;border-radius:8px;" title="${alt} (CodeSandbox)" loading="lazy" allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking" sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"></iframe>`;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Standard docfx code-view with iframe-src + data-demos-base-url.
|
|
182
|
+
// Output the pre-rendered widget shell; sample-widget.ts activates it at runtime.
|
|
183
|
+
if (srcMatch) {
|
|
184
|
+
const src = replaceEnvVars(srcMatch[1]);
|
|
185
|
+
if (!src || src.includes('{environment:')) return ''; // env var not resolved
|
|
186
|
+
const demosBaseUrl = demosBaseMatch ? replaceEnvVars(demosBaseMatch[1]) : '';
|
|
187
|
+
const githubSrc = githubSrcMatch ? githubSrcMatch[1] : '';
|
|
188
|
+
const platform = process.env.DOCS_PLATFORM || 'angular';
|
|
189
|
+
return buildWidgetHtml(src, demosBaseUrl, githubSrc, height, alt, platform);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
return '';
|
|
193
|
+
}
|
|
194
|
+
);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Transform <div class="divider--half"></div> into <hr>
|
|
199
|
+
*/
|
|
200
|
+
function transformDividers(html: string): string {
|
|
201
|
+
return html.replace(/<div\s+class="divider--half"\s*>\s*<\/div>/g, '<hr/>');
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function strVal(v: unknown): string {
|
|
205
|
+
return typeof v === 'string' ? v : Array.isArray(v) ? v.join(' ') : String(v ?? '');
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Rehype plugin — transforms <code-view> elements into the pre-rendered widget shell.
|
|
210
|
+
*
|
|
211
|
+
* Handles two HAST representations depending on what's in the pipeline:
|
|
212
|
+
*
|
|
213
|
+
* 1. `element` nodes (tagName === 'code-view') — produced when rehype-raw is
|
|
214
|
+
* present and re-parses raw HTML fragments into proper HAST elements.
|
|
215
|
+
*
|
|
216
|
+
* 2. `raw` string nodes — produced when rehype-raw is NOT in the pipeline
|
|
217
|
+
* (e.g. Astro content-collection .md files). In that case multiline
|
|
218
|
+
* <code-view> blocks are opaque raw strings; we run the same regex
|
|
219
|
+
* transformer used in the remark stage.
|
|
220
|
+
*/
|
|
221
|
+
export function rehypeCodeView() {
|
|
222
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
223
|
+
return (tree: any) => {
|
|
224
|
+
// ── 1. element nodes (after rehype-raw) ──────────────────────────────────
|
|
225
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
226
|
+
visit(tree, 'element', (node: any) => {
|
|
227
|
+
if (node.tagName !== 'code-view') return;
|
|
228
|
+
|
|
229
|
+
const p = node.properties || {};
|
|
230
|
+
|
|
231
|
+
// HAST converts hyphenated attributes to camelCase via property-information.
|
|
232
|
+
const iframeSrc = strVal(p.iframeSrc ?? p['iframe-src'] ?? '');
|
|
233
|
+
const demosBase = strVal(p.dataDemosBaseUrl ?? p['data-demos-base-url'] ?? '');
|
|
234
|
+
const githubSrc = strVal(p.githubSrc ?? p['github-src'] ?? '');
|
|
235
|
+
const styleStr = strVal(p.style ?? '');
|
|
236
|
+
const alt = strVal(p.alt ?? 'Demo');
|
|
237
|
+
|
|
238
|
+
const src = replaceEnvVars(iframeSrc);
|
|
239
|
+
const baseUrl = replaceEnvVars(demosBase);
|
|
240
|
+
|
|
241
|
+
if (!src || src.includes('{environment:')) return;
|
|
242
|
+
|
|
243
|
+
const heightMatch = styleStr.match(/height:\s*(\d+\s*px)/i);
|
|
244
|
+
const height = heightMatch ? heightMatch[1].replace(/\s+/, '') : '400px';
|
|
245
|
+
const platform = process.env.DOCS_PLATFORM || 'angular';
|
|
246
|
+
const widgetId = 'cw' + src.replace(/[^a-z0-9]/gi, '-');
|
|
247
|
+
const tabId = `${widgetId}-example`;
|
|
248
|
+
|
|
249
|
+
node.tagName = 'div';
|
|
250
|
+
node.properties = {
|
|
251
|
+
className: ['code-view'],
|
|
252
|
+
id: widgetId,
|
|
253
|
+
'data-iframe-src': src,
|
|
254
|
+
'data-platform': platform,
|
|
255
|
+
...(baseUrl ? { 'data-demos-base-url': baseUrl } : {}),
|
|
256
|
+
...(githubSrc ? { 'data-github-src': githubSrc } : {}),
|
|
257
|
+
};
|
|
258
|
+
node.children = [
|
|
259
|
+
{
|
|
260
|
+
type: 'element', tagName: 'div',
|
|
261
|
+
properties: { className: ['code-view-navbar'] },
|
|
262
|
+
children: [
|
|
263
|
+
{
|
|
264
|
+
type: 'element', tagName: 'div',
|
|
265
|
+
properties: { className: ['code-view-tab', 'code-view-tab--active'], 'data-tab-id': tabId },
|
|
266
|
+
children: [{ type: 'text', value: 'EXAMPLE' }],
|
|
267
|
+
},
|
|
268
|
+
{
|
|
269
|
+
type: 'element', tagName: 'span',
|
|
270
|
+
properties: { className: ['fs-button-container'], title: 'Expand to fullscreen' },
|
|
271
|
+
children: [],
|
|
272
|
+
},
|
|
273
|
+
],
|
|
274
|
+
},
|
|
275
|
+
{
|
|
276
|
+
type: 'element', tagName: 'div',
|
|
277
|
+
properties: { className: ['code-views-container'] },
|
|
278
|
+
children: [
|
|
279
|
+
{
|
|
280
|
+
type: 'element', tagName: 'div',
|
|
281
|
+
properties: {
|
|
282
|
+
id: tabId,
|
|
283
|
+
className: ['sample-container', 'code-view-tab-content', 'loading'],
|
|
284
|
+
style: `height: ${height}`,
|
|
285
|
+
},
|
|
286
|
+
children: [
|
|
287
|
+
{
|
|
288
|
+
type: 'element', tagName: 'iframe',
|
|
289
|
+
properties: {
|
|
290
|
+
'data-src': src,
|
|
291
|
+
title: alt,
|
|
292
|
+
style: 'width: 100%; height: 100%;',
|
|
293
|
+
frameBorder: '0',
|
|
294
|
+
seamless: '',
|
|
295
|
+
},
|
|
296
|
+
children: [],
|
|
297
|
+
},
|
|
298
|
+
],
|
|
299
|
+
},
|
|
300
|
+
],
|
|
301
|
+
},
|
|
302
|
+
];
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// ── 2. raw string nodes (no rehype-raw in pipeline) ──────────────────────
|
|
306
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
307
|
+
visit(tree, 'raw', (node: any) => {
|
|
308
|
+
if (!node.value || !node.value.includes('<code-view')) return;
|
|
309
|
+
const updated = transformCodeView(replaceEnvVars(node.value as string));
|
|
310
|
+
if (updated !== node.value) node.value = updated;
|
|
311
|
+
});
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
export function remarkDocfx() {
|
|
316
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
317
|
+
return (tree: any, file: any) => {
|
|
318
|
+
const filePath = (file.path as string) ?? '';
|
|
319
|
+
const docsDir = process.env.DOCS_SOURCE_PATH
|
|
320
|
+
? path.resolve(process.env.DOCS_SOURCE_PATH)
|
|
321
|
+
: (filePath ? path.dirname(filePath) : '');
|
|
322
|
+
// 1. Walk the AST and replace environment variables in text/links/html
|
|
323
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
324
|
+
visit(tree, (node: any) => {
|
|
325
|
+
// Text nodes
|
|
326
|
+
if (node.type === 'text' && node.value) {
|
|
327
|
+
node.value = replaceEnvVars(node.value as string);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Links
|
|
331
|
+
if (node.type === 'link' && node.url) {
|
|
332
|
+
node.url = replaceEnvVars(node.url as string);
|
|
333
|
+
node.url = rewriteMdLink(node.url as string, filePath, docsDir);
|
|
334
|
+
// Prepend DOCS_BASE to root-relative internal links that were not already
|
|
335
|
+
// rewritten by rewriteMdLink (e.g. bare /grids/grid/… links that skip
|
|
336
|
+
// the .md-only rewriter above).
|
|
337
|
+
const docsBase = (process.env.DOCS_BASE ?? '').replace(/\/$/, '');
|
|
338
|
+
if (
|
|
339
|
+
docsBase &&
|
|
340
|
+
(node.url as string).startsWith('/') &&
|
|
341
|
+
!(node.url as string).startsWith('//') &&
|
|
342
|
+
!(node.url as string).startsWith(docsBase + '/')
|
|
343
|
+
) {
|
|
344
|
+
node.url = docsBase + (node.url as string);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// Images
|
|
349
|
+
if (node.type === 'image' && node.url) {
|
|
350
|
+
node.url = replaceEnvVars(node.url as string);
|
|
351
|
+
node.url = (node.url as string).replace(/^(\.\.\/)+images\//, '/images/');
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Code blocks — normalize language identifiers to lowercase
|
|
355
|
+
if (node.type === 'code' && node.lang) {
|
|
356
|
+
node.lang = (node.lang as string).toLowerCase();
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
// Inline HTML
|
|
360
|
+
if (node.type === 'html' && node.value) {
|
|
361
|
+
node.value = replaceEnvVars(node.value as string);
|
|
362
|
+
node.value = transformCodeView(node.value as string);
|
|
363
|
+
node.value = transformDividers(node.value as string);
|
|
364
|
+
node.value = (node.value as string).replace(/src="(\.\.\/)+images\//g, 'src="/images/');
|
|
365
|
+
}
|
|
366
|
+
});
|
|
367
|
+
};
|
|
368
|
+
}
|