astro-accelerator 7.0.10 → 7.1.12

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,88 +1,123 @@
1
1
  ---
2
2
  // warning: This file is overwritten by Astro Accelerator
3
3
 
4
- import { Accelerator } from 'astro-accelerator-utils';
5
- import type { Frontmatter } from 'astro-accelerator-utils/types/Frontmatter';
6
- import { SITE, OPEN_GRAPH, HEADER_SCRIPTS } from '@config';
4
+ import { Accelerator } from "astro-accelerator-utils";
5
+ import type { Frontmatter } from "astro-accelerator-utils/types/Frontmatter";
6
+ import { SITE, OPEN_GRAPH, HEADER_SCRIPTS } from "@config";
7
7
 
8
8
  const accelerator = new Accelerator(SITE);
9
- const stats = new accelerator.statistics('accelerator/components/HtmlHead.astro');
9
+ const stats = new accelerator.statistics(
10
+ "accelerator/components/HtmlHead.astro",
11
+ );
10
12
  stats.start();
11
13
 
12
14
  // Properties
13
15
  type Props = {
14
16
  lang: string;
15
- frontmatter: Frontmatter
16
- headings: { depth: number; slug: string; text: string; }[];
17
+ frontmatter: Frontmatter;
18
+ headings: { depth: number; slug: string; text: string }[];
17
19
  };
18
20
  const { frontmatter } = Astro.props;
19
21
 
20
22
  // Logic
21
23
  const imageSrc = frontmatter.bannerImage?.src ?? OPEN_GRAPH.image.src;
22
24
  const imageAlt = frontmatter.bannerImage?.alt ?? OPEN_GRAPH.image.alt;
23
- const robots = frontmatter.robots ?? 'index, follow';
25
+ const robots = frontmatter.robots ?? "index, follow";
24
26
  const canonicalImageSrc = new URL(imageSrc, Astro.site);
25
- const canonicalURL = accelerator.urlFormatter.formatUrl(new URL(Astro.url.pathname, Astro.site + SITE.subfolder));
27
+ const canonicalURL = accelerator.urlFormatter.formatUrl(
28
+ new URL(Astro.url.pathname, Astro.site + SITE.subfolder),
29
+ );
26
30
  const socialTitle = await accelerator.markdown.getTextFrom(frontmatter?.title);
27
- const title = `${ accelerator.markdown.titleCase(socialTitle) } ${ ((frontmatter.titleAdditional) ? ` ${frontmatter.titleAdditional}` : '') } | ${ SITE.title }`;
28
- const pageMeta = (frontmatter?.meta && frontmatter?.meta?.length > 0)
29
- ? frontmatter.meta
30
- : [];
31
+ const title = `${accelerator.markdown.titleCase(socialTitle)} ${frontmatter.titleAdditional ? ` ${frontmatter.titleAdditional}` : ""} | ${SITE.title}`;
32
+ const pageMeta =
33
+ frontmatter?.meta && frontmatter?.meta?.length > 0 ? frontmatter.meta : [];
31
34
 
32
35
  const authorList = accelerator.authors.forPost(frontmatter);
33
- const authorMeta = (authorList.mainAuthor?.frontmatter?.meta && authorList.mainAuthor?.frontmatter?.meta?.length > 0)
34
- ? authorList.mainAuthor.frontmatter.meta
35
- : [];
36
+ const authorMeta =
37
+ authorList.mainAuthor?.frontmatter?.meta &&
38
+ authorList.mainAuthor?.frontmatter?.meta?.length > 0
39
+ ? authorList.mainAuthor.frontmatter.meta
40
+ : [];
36
41
 
37
42
  const autoMeta = (name: string) => {
38
- return pageMeta.filter(m => m.name.toLowerCase() === name).length === 0;
39
- }
43
+ return pageMeta.filter((m) => m.name.toLowerCase() === name).length === 0;
44
+ };
40
45
 
41
46
  stats.stop();
42
47
  ---
48
+
43
49
  <head>
44
50
  <meta charset="utf-8" />
45
- <title>{ title }</title>
46
- <meta name="robots" content={ 'max-image-preview:large, ' + robots }>
47
- <meta name="keywords" content={ frontmatter.keywords }>
48
- <meta name="description" content={ frontmatter.description }>
49
- <link rel="stylesheet" href={ SITE.subfolder + '/css/vars.css' }>
50
- <link rel="stylesheet" href={ SITE.subfolder + '/css/main.css' }>
51
- {autoMeta('viewport') &&
52
- <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
53
- }
54
- {autoMeta('format-detection') &&
55
- <meta name="format-detection" content="telephone=no">
51
+ <title>{title}</title>
52
+ <meta name="robots" content={"max-image-preview:large, " + robots} />
53
+ <meta name="keywords" content={frontmatter.keywords} />
54
+ <meta name="description" content={frontmatter.description} />
55
+ <link
56
+ rel="stylesheet"
57
+ href={SITE.subfolder + `/css/vars.7.1.12.css`}
58
+ data-versioned
59
+ />
60
+ <link
61
+ rel="stylesheet"
62
+ href={SITE.subfolder + `/css/main.7.1.12.css`}
63
+ data-versioned
64
+ />
65
+ {
66
+ autoMeta("viewport") && (
67
+ <meta
68
+ name="viewport"
69
+ content="width=device-width, initial-scale=1, minimum-scale=1"
70
+ />
71
+ )
56
72
  }
57
- {autoMeta('theme-color') &&
58
- <meta name="theme-color" content={ SITE.themeColor }>
73
+ {
74
+ autoMeta("format-detection") && (
75
+ <meta name="format-detection" content="telephone=no" />
76
+ )
59
77
  }
60
- {autoMeta('canonical') &&
61
- <link rel="canonical" href={ canonicalURL }>
78
+ {
79
+ autoMeta("theme-color") && (
80
+ <meta name="theme-color" content={SITE.themeColor} />
81
+ )
62
82
  }
63
- {SITE.feedUrl &&
64
- <link rel="alternate" type="application/atom+xml" title={ SITE.title } href={ SITE.feedUrl }>
83
+ {autoMeta("canonical") && <link rel="canonical" href={canonicalURL} />}
84
+ {
85
+ SITE.feedUrl && (
86
+ <link
87
+ rel="alternate"
88
+ type="application/atom+xml"
89
+ title={SITE.title}
90
+ href={SITE.feedUrl}
91
+ />
92
+ )
65
93
  }
66
- <link rel="alternate" href={ SITE.subfolder + '/sitemap.xml' } type="application/rss+xml">
67
- <link rel="shortcut icon" href={ SITE.subfolder + '/icons/favicon.ico' } type="image/x-icon">
68
- <link rel="apple-touch-icon" href={ SITE.subfolder + '/icons/apple-touch-icon.png' }>
69
- <meta property="og:type" content="website">
70
- <meta property="og:url" content={ canonicalURL }>
71
- <meta property="og:title" content={ socialTitle }>
72
- <meta property="og:description" content={ frontmatter.description }>
73
- <meta property="og:image" content={ canonicalImageSrc }>
74
- <meta property="og:image:alt" content={ imageAlt }>
75
- <meta name="twitter:card" content="summary_large_image">
76
- <meta name="twitter:title" content={ socialTitle }>
77
- <meta name="twitter:description" content={ frontmatter.description }>
78
- <meta name="twitter:image" content={ canonicalImageSrc }>
79
- <meta name="twitter:image:alt" content={ imageAlt }>
80
- <meta name="view-transition" content="same-origin">
81
- {pageMeta.map(m =>
82
- <meta name={ m.name } content={ m.content }>
83
- )}
84
- {authorMeta.map(m =>
85
- <meta name={ m.name } content={ m.content }>
86
- )}
87
- <Fragment set:html={ HEADER_SCRIPTS }></Fragment>
88
- </head>
94
+ <link
95
+ rel="alternate"
96
+ href={SITE.subfolder + "/sitemap.xml"}
97
+ type="application/rss+xml"
98
+ />
99
+ <link
100
+ rel="shortcut icon"
101
+ href={SITE.subfolder + "/icons/favicon.ico"}
102
+ type="image/x-icon"
103
+ />
104
+ <link
105
+ rel="apple-touch-icon"
106
+ href={SITE.subfolder + "/icons/apple-touch-icon.png"}
107
+ />
108
+ <meta property="og:type" content="website" />
109
+ <meta property="og:url" content={canonicalURL} />
110
+ <meta property="og:title" content={socialTitle} />
111
+ <meta property="og:description" content={frontmatter.description} />
112
+ <meta property="og:image" content={canonicalImageSrc} />
113
+ <meta property="og:image:alt" content={imageAlt} />
114
+ <meta name="twitter:card" content="summary_large_image" />
115
+ <meta name="twitter:title" content={socialTitle} />
116
+ <meta name="twitter:description" content={frontmatter.description} />
117
+ <meta name="twitter:image" content={canonicalImageSrc} />
118
+ <meta name="twitter:image:alt" content={imageAlt} />
119
+ <meta name="view-transition" content="same-origin" />
120
+ {pageMeta.map((m) => <meta name={m.name} content={m.content} />)}
121
+ {authorMeta.map((m) => <meta name={m.name} content={m.content} />)}
122
+ <Fragment set:html={HEADER_SCRIPTS} />
123
+ </head>
@@ -0,0 +1,165 @@
1
+ /**
2
+ * This javascript file comes from Astro Accelerator
3
+ * Edits will be overwritten if you change the file locally
4
+ *
5
+ * Copies versioned CSS files and rewrites data-versioned links in HtmlHead.
6
+ *
7
+ * @format
8
+ */
9
+
10
+ import fs from 'fs';
11
+ import path from 'path';
12
+
13
+ const workingDirectory = process.cwd();
14
+ const defaultHtmlHead = path.join(
15
+ 'src',
16
+ 'themes',
17
+ 'accelerator',
18
+ 'components',
19
+ 'HtmlHead.astro'
20
+ );
21
+
22
+ const htmlHeadArg = process.argv[2];
23
+ const htmlHeadPath = path.resolve(
24
+ workingDirectory,
25
+ htmlHeadArg ?? defaultHtmlHead
26
+ );
27
+
28
+ const packageJsonPath = path.join(workingDirectory, 'package.json');
29
+ const pkg = JSON.parse(await fs.promises.readFile(packageJsonPath, 'utf8'));
30
+ const version = pkg.version;
31
+
32
+ if (!version) {
33
+ console.error('No version found in package.json');
34
+ process.exit(1);
35
+ }
36
+
37
+ const cssDir = path.join(workingDirectory, 'public', 'css');
38
+ const previousVersionLimit = 5;
39
+ const linkRegex = /<link\b[\s\S]*?\bdata-versioned\b[\s\S]*?\/>/g;
40
+ const hrefCssRegex = /`(\/css\/[^`]+)`/;
41
+
42
+ /**
43
+ * Derive the unversioned base filename from a CSS path in an href.
44
+ * e.g. /css/vars.7.1.11.css → vars.css
45
+ * /css/main.css → main.css
46
+ */
47
+ function getBaseCssName(cssPath) {
48
+ const withoutQuery = cssPath.split('?')[0];
49
+ const fileName = path.basename(withoutQuery);
50
+
51
+ if (!fileName.endsWith('.css')) {
52
+ return null;
53
+ }
54
+
55
+ const withoutExt = fileName.slice(0, -'.css'.length);
56
+ const versionSuffix = `.${version}`;
57
+
58
+ if (withoutExt.endsWith(versionSuffix)) {
59
+ return withoutExt.slice(0, -versionSuffix.length) + '.css';
60
+ }
61
+
62
+ // Strip a semver-like suffix if present (e.g. main.7.1.10.css)
63
+ const semverMatch = withoutExt.match(
64
+ /^(.+)\.(\d+\.\d+\.\d+(?:-[0-9A-Za-z.-]+)?)$/
65
+ );
66
+ if (semverMatch) {
67
+ return semverMatch[1] + '.css';
68
+ }
69
+
70
+ return fileName;
71
+ }
72
+
73
+ async function cleanStaleVersions(baseName) {
74
+ const baseWithoutExt = baseName.slice(0, -'.css'.length);
75
+ const currentVersioned = `${baseWithoutExt}.${version}.css`;
76
+ const entries = await fs.promises.readdir(cssDir);
77
+ const escapedBase = baseWithoutExt.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
78
+ const versionedPattern = new RegExp(
79
+ `^${escapedBase}\\.(\\d+\\.\\d+\\.\\d+(?:-[0-9A-Za-z.-]+)?)\\.css$`
80
+ );
81
+ const previousVersions = entries
82
+ .map((entry) => {
83
+ const match = entry.match(versionedPattern);
84
+ return match ? { entry, version: match[1] } : null;
85
+ })
86
+ .filter((item) => item !== null && item.entry !== currentVersioned)
87
+ .sort((a, b) =>
88
+ b.version.localeCompare(a.version, undefined, { numeric: true })
89
+ );
90
+
91
+ for (const { entry } of previousVersions.slice(previousVersionLimit)) {
92
+ await fs.promises.unlink(path.join(cssDir, entry));
93
+ console.log(`Removed stale ${entry}`);
94
+ }
95
+ }
96
+
97
+ async function copyVersionedCss(baseName) {
98
+ const sourcePath = path.join(cssDir, baseName);
99
+ const baseWithoutExt = baseName.slice(0, -'.css'.length);
100
+ const versionedName = `${baseWithoutExt}.${version}.css`;
101
+ const destPath = path.join(cssDir, versionedName);
102
+
103
+ try {
104
+ await fs.promises.access(sourcePath);
105
+ } catch {
106
+ console.error(`Source CSS not found: ${sourcePath}`);
107
+ process.exit(1);
108
+ }
109
+
110
+ await fs.promises.copyFile(sourcePath, destPath);
111
+ console.log(`Copied ${baseName} → ${versionedName}`);
112
+ await cleanStaleVersions(baseName);
113
+
114
+ return versionedName;
115
+ }
116
+
117
+ function rewriteLinkHref(linkTag, versionedName) {
118
+ return linkTag.replace(hrefCssRegex, `\`/css/${versionedName}\``);
119
+ }
120
+
121
+ let content = await fs.promises.readFile(htmlHeadPath, 'utf8');
122
+ const links = [...content.matchAll(linkRegex)];
123
+
124
+ if (links.length === 0) {
125
+ console.error(
126
+ `No data-versioned <link> tags found in ${htmlHeadPath}`
127
+ );
128
+ process.exit(1);
129
+ }
130
+
131
+ const processedBases = new Set();
132
+
133
+ for (const match of links) {
134
+ const linkTag = match[0];
135
+ const hrefMatch = linkTag.match(hrefCssRegex);
136
+
137
+ if (!hrefMatch) {
138
+ console.error(
139
+ 'data-versioned link is missing a `/css/...` href template:',
140
+ linkTag
141
+ );
142
+ process.exit(1);
143
+ }
144
+
145
+ const baseName = getBaseCssName(hrefMatch[1]);
146
+ if (!baseName) {
147
+ console.error(`Could not derive base CSS name from ${hrefMatch[1]}`);
148
+ process.exit(1);
149
+ }
150
+
151
+ let versionedName;
152
+ if (processedBases.has(baseName)) {
153
+ const baseWithoutExt = baseName.slice(0, -'.css'.length);
154
+ versionedName = `${baseWithoutExt}.${version}.css`;
155
+ } else {
156
+ versionedName = await copyVersionedCss(baseName);
157
+ processedBases.add(baseName);
158
+ }
159
+
160
+ const updatedLink = rewriteLinkHref(linkTag, versionedName);
161
+ content = content.replace(linkTag, updatedLink);
162
+ }
163
+
164
+ await fs.promises.writeFile(htmlHeadPath, content, 'utf8');
165
+ console.log(`Updated ${htmlHeadPath}`);