starlight-cannoli-plugins 1.0.3 → 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,31 +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 as dirname2, join, relative, resolve as resolve2 } from "path";
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
- return "/" + resolve(resolvedPath).slice(siteRoot.length).replace(/\\/g, "/");
28
- }
29
16
 
30
17
  // src/plugins/rehype-validate-links.ts
31
18
  function getResolvedLink(href, currentFilePath) {
@@ -55,10 +42,10 @@ function getResolvedLink(href, currentFilePath) {
55
42
  relativePath = withoutFragment.slice(1);
56
43
  projectAbsolute = join(PROJECT_DOCS_DIR, relativePath);
57
44
  } else {
58
- const currentFileDir = dirname2(currentFilePath);
59
- const resolvedAbsPath = resolve2(currentFileDir, withoutFragment);
45
+ const currentFileDir = dirname(currentFilePath);
46
+ const resolvedAbsPath = resolve(currentFileDir, withoutFragment);
60
47
  projectAbsolute = resolvedAbsPath;
61
- const docsRootAbsolute = resolve2(PROJECT_DOCS_DIR);
48
+ const docsRootAbsolute = resolve(PROJECT_DOCS_DIR);
62
49
  relativePath = relative(docsRootAbsolute, resolvedAbsPath);
63
50
  }
64
51
  const hasExtension = /\.[a-z0-9]+$/i.test(projectAbsolute);
@@ -129,7 +116,6 @@ function rehypeValidateLinks(options) {
129
116
  var rehype_validate_links_default = rehypeValidateLinks;
130
117
 
131
118
  export {
132
- normalizePath,
133
119
  rehypeValidateLinks,
134
120
  rehype_validate_links_default
135
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-H377D3RC.js";
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 = normalizePath(src, htmlFile, dir.pathname);
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 = normalizePath(href, htmlFile, dir.pathname);
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,
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  rehypeValidateLinks,
3
3
  rehype_validate_links_default
4
- } from "../chunk-H377D3RC.js";
4
+ } from "../chunk-HRUZQZ5L.js";
5
5
  export {
6
6
  rehype_validate_links_default as default,
7
7
  rehypeValidateLinks
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "starlight-cannoli-plugins",
3
3
  "type": "module",
4
- "version": "1.0.3",
4
+ "version": "1.0.5",
5
5
  "description": "Starlight plugins for automatic sidebar generation and link validation",
6
6
  "license": "ISC",
7
7
  "main": "./dist/index.js",