starlight-cannoli-plugins 1.0.4 → 1.0.5
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.
|
@@ -1,32 +1,18 @@
|
|
|
1
1
|
// src/plugins/rehype-validate-links.ts
|
|
2
2
|
import { existsSync } from "fs";
|
|
3
3
|
import { sync as globSync } from "glob";
|
|
4
|
-
import { dirname
|
|
4
|
+
import { dirname, join, relative, resolve } from "path";
|
|
5
5
|
import { visit } from "unist-util-visit";
|
|
6
6
|
|
|
7
7
|
// src/plugins/utils/path-utils.ts
|
|
8
|
-
import { dirname, resolve } from "path";
|
|
9
8
|
import { minimatch } from "minimatch";
|
|
10
9
|
var PROJECT_DOCS_DIR = "src/content/docs";
|
|
11
|
-
function isExternalPath(path) {
|
|
12
|
-
return path.startsWith("http") || path.startsWith("data:") || path.startsWith("/");
|
|
13
|
-
}
|
|
14
10
|
function matchesSkipPattern(path, patterns) {
|
|
15
11
|
if (!patterns || patterns.length === 0) {
|
|
16
12
|
return false;
|
|
17
13
|
}
|
|
18
14
|
return patterns.some((pattern) => minimatch(path, pattern));
|
|
19
15
|
}
|
|
20
|
-
function normalizePath(path, fromFilePath, siteRootPath) {
|
|
21
|
-
if (isExternalPath(path)) {
|
|
22
|
-
return null;
|
|
23
|
-
}
|
|
24
|
-
const fileDir = dirname(fromFilePath);
|
|
25
|
-
const resolvedPath = resolve(fileDir, path);
|
|
26
|
-
const siteRoot = resolve(siteRootPath);
|
|
27
|
-
const relativePath = resolve(resolvedPath).slice(siteRoot.length).replace(/\\/g, "/").replace(/^\/+/, "");
|
|
28
|
-
return "/" + relativePath;
|
|
29
|
-
}
|
|
30
16
|
|
|
31
17
|
// src/plugins/rehype-validate-links.ts
|
|
32
18
|
function getResolvedLink(href, currentFilePath) {
|
|
@@ -56,10 +42,10 @@ function getResolvedLink(href, currentFilePath) {
|
|
|
56
42
|
relativePath = withoutFragment.slice(1);
|
|
57
43
|
projectAbsolute = join(PROJECT_DOCS_DIR, relativePath);
|
|
58
44
|
} else {
|
|
59
|
-
const currentFileDir =
|
|
60
|
-
const resolvedAbsPath =
|
|
45
|
+
const currentFileDir = dirname(currentFilePath);
|
|
46
|
+
const resolvedAbsPath = resolve(currentFileDir, withoutFragment);
|
|
61
47
|
projectAbsolute = resolvedAbsPath;
|
|
62
|
-
const docsRootAbsolute =
|
|
48
|
+
const docsRootAbsolute = resolve(PROJECT_DOCS_DIR);
|
|
63
49
|
relativePath = relative(docsRootAbsolute, resolvedAbsPath);
|
|
64
50
|
}
|
|
65
51
|
const hasExtension = /\.[a-z0-9]+$/i.test(projectAbsolute);
|
|
@@ -130,7 +116,6 @@ function rehypeValidateLinks(options) {
|
|
|
130
116
|
var rehype_validate_links_default = rehypeValidateLinks;
|
|
131
117
|
|
|
132
118
|
export {
|
|
133
|
-
normalizePath,
|
|
134
119
|
rehypeValidateLinks,
|
|
135
120
|
rehype_validate_links_default
|
|
136
121
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -8,6 +8,9 @@ import 'vfile';
|
|
|
8
8
|
/**
|
|
9
9
|
* Astro integration that normalizes img src and anchor href attributes to absolute paths in HTML files after build.
|
|
10
10
|
* This processes resources in Starlight docs that aren't handled by the rehype plugin.
|
|
11
|
+
*
|
|
12
|
+
* Handles the case where Astro converts markdown files into index.html files in subdirectories,
|
|
13
|
+
* which changes the relative path structure.
|
|
11
14
|
*/
|
|
12
15
|
declare function astroNormalizePaths(): AstroIntegration;
|
|
13
16
|
|
package/dist/index.js
CHANGED
|
@@ -2,13 +2,13 @@ import {
|
|
|
2
2
|
starlightIndexOnlySidebar
|
|
3
3
|
} from "./chunk-NTIYGHZG.js";
|
|
4
4
|
import {
|
|
5
|
-
normalizePath,
|
|
6
5
|
rehypeValidateLinks
|
|
7
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-HRUZQZ5L.js";
|
|
8
7
|
|
|
9
8
|
// src/plugins/astro-normalize-paths.ts
|
|
10
|
-
import { readFileSync, writeFileSync } from "fs";
|
|
9
|
+
import { readFileSync, writeFileSync, existsSync } from "fs";
|
|
11
10
|
import { sync as globSync } from "glob";
|
|
11
|
+
import { dirname, resolve } from "path";
|
|
12
12
|
function astroNormalizePaths() {
|
|
13
13
|
return {
|
|
14
14
|
name: "astro-normalize-paths",
|
|
@@ -23,8 +23,12 @@ function astroNormalizePaths() {
|
|
|
23
23
|
while ((match = imgRegex.exec(content)) !== null) {
|
|
24
24
|
const attrs = match[1];
|
|
25
25
|
const src = match[2];
|
|
26
|
-
const normalized =
|
|
26
|
+
const normalized = normalizeAssetPath(src, htmlFile, dir.pathname);
|
|
27
27
|
if (normalized && src !== normalized) {
|
|
28
|
+
console.log(`[astro-normalize-paths] Img path resolution:`);
|
|
29
|
+
console.log(` Original: ${src}`);
|
|
30
|
+
console.log(` HTML file: ${htmlFile}`);
|
|
31
|
+
console.log(` Normalized: ${normalized}`);
|
|
28
32
|
const oldTag = `<img${attrs}src="${src}"`;
|
|
29
33
|
const newTag = `<img${attrs}src="${normalized}"`;
|
|
30
34
|
content = content.replace(oldTag, newTag);
|
|
@@ -34,7 +38,7 @@ function astroNormalizePaths() {
|
|
|
34
38
|
while ((match = anchorRegex.exec(content)) !== null) {
|
|
35
39
|
const attrs = match[1];
|
|
36
40
|
const href = match[2];
|
|
37
|
-
const normalized =
|
|
41
|
+
const normalized = normalizeAssetPath(href, htmlFile, dir.pathname);
|
|
38
42
|
if (normalized && href !== normalized) {
|
|
39
43
|
const oldTag = `<a${attrs}href="${href}"`;
|
|
40
44
|
const newTag = `<a${attrs}href="${normalized}"`;
|
|
@@ -49,6 +53,24 @@ function astroNormalizePaths() {
|
|
|
49
53
|
}
|
|
50
54
|
};
|
|
51
55
|
}
|
|
56
|
+
function normalizeAssetPath(path, htmlFile, siteRootPath) {
|
|
57
|
+
if (path.startsWith("http") || path.startsWith("data:") || path.startsWith("/")) {
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
const htmlDir = dirname(htmlFile);
|
|
61
|
+
const resolvedPath = resolve(htmlDir, path);
|
|
62
|
+
const siteRoot = resolve(siteRootPath);
|
|
63
|
+
let absolutePath = resolvedPath.slice(siteRoot.length).replace(/\\/g, "/");
|
|
64
|
+
if (!existsSync(resolvedPath)) {
|
|
65
|
+
const parentDir = dirname(htmlDir);
|
|
66
|
+
const alternativePath = resolve(parentDir, path);
|
|
67
|
+
if (existsSync(alternativePath)) {
|
|
68
|
+
absolutePath = alternativePath.slice(siteRoot.length).replace(/\\/g, "/");
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
const finalPath = "/" + absolutePath.replace(/^\/+/, "");
|
|
72
|
+
return finalPath;
|
|
73
|
+
}
|
|
52
74
|
export {
|
|
53
75
|
astroNormalizePaths,
|
|
54
76
|
rehypeValidateLinks,
|
package/package.json
CHANGED