@inizioevoke/astro-core 1.0.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 (29) hide show
  1. package/package.json +31 -0
  2. package/src/components/Modal/Modal.astro +36 -0
  3. package/src/components/Modal/Modal.css +145 -0
  4. package/src/components/Modal/Modal.ts +109 -0
  5. package/src/components/Modal/ModalOverlay.astro +1 -0
  6. package/src/components/Modal/ModalTrigger.astro +29 -0
  7. package/src/components/PdfViewer/PdfViewer.astro +103 -0
  8. package/src/components/PdfViewer/PdfViewer.css +198 -0
  9. package/src/components/PdfViewer/PdfViewer.ts +183 -0
  10. package/src/components/PdfViewer/pdf.min.mjs +28 -0
  11. package/src/components/PdfViewer/pdf.mjs +26465 -0
  12. package/src/components/PdfViewer/pdf.mjs.map +1 -0
  13. package/src/components/PdfViewer/pdf.worker.min.mjs +28 -0
  14. package/src/components/PdfViewer/pdf.worker.mjs +63057 -0
  15. package/src/components/PdfViewer/pdf.worker.mjs.map +1 -0
  16. package/src/components/ScrollContainer/ScrollContainer.astro +55 -0
  17. package/src/components/ScrollContainer/ScrollContainer.css +64 -0
  18. package/src/components/ScrollContainer/ScrollContainer.ts +143 -0
  19. package/src/components/Tabs/TabItem.astro +24 -0
  20. package/src/components/Tabs/Tabs.astro +94 -0
  21. package/src/components/Tabs/Tabs.css +70 -0
  22. package/src/components/Tabs/Tabs.ts +22 -0
  23. package/src/components/Zoom/Zoom.astro +64 -0
  24. package/src/components/Zoom/ZoomContent.astro +0 -0
  25. package/src/components/Zoom/ZoomModal.astro +0 -0
  26. package/src/components/index.ts +8 -0
  27. package/src/integrations/index.ts +2 -0
  28. package/src/integrations/prune-build.ts +71 -0
  29. package/src/integrations/relative-links.ts +178 -0
@@ -0,0 +1,178 @@
1
+ import type { AstroIntegration, AstroConfig } from 'astro';
2
+ import { readdir, readFile, writeFile } from 'node:fs/promises';
3
+ import path from 'path';
4
+ import { fileURLToPath } from 'url';
5
+
6
+ // adapted from https://github.com/ixkaito/astro-relative-links/blob/main/src/index.ts
7
+
8
+ /**
9
+ * Add leading and trailing slashes to the `base`.
10
+ *
11
+ * @param {string} base
12
+ * @returns {string} - Formatted base.
13
+ */
14
+ export function leadingTrailingSlash(base?: string) {
15
+ return base?.replace(/^\/*([^\/]+)(.*)([^\/]+)\/*$/, '/$1$2$3/') || '/';
16
+ }
17
+
18
+ const pattern = {
19
+ htmlAttr: `(href|src(set)?|poster|component-url|renderer-url)=["']?([^"']*,)?`,
20
+ styleAttr: `style=("[^"]*|'[^']*|[^\\s]*)`,
21
+ styleUrl: `url\\(\\s*?["']?`,
22
+ };
23
+
24
+ /**
25
+ * Replace absolute paths in HTML files with relative paths.
26
+ *
27
+ * @param {object} options
28
+ * @param {string} options.outDirPath - The path of the directory that `astro build` writes final build to.
29
+ * @param {string} options.filePath - The path of the target file.
30
+ * @param {string} options.base - The base path to deploy to.
31
+ * @param {string} options.html - The content of the HTML file.
32
+ * @returns {string} - Replaced HTML
33
+ */
34
+ function replaceHTML({
35
+ outDirPath,
36
+ filePath,
37
+ base,
38
+ html,
39
+ }: {
40
+ outDirPath: string;
41
+ filePath: string;
42
+ base: string;
43
+ html: string;
44
+ }) {
45
+ const { htmlAttr, styleAttr, styleUrl } = pattern;
46
+ const htmlPattern =
47
+ `<[^>]+\\s(` + htmlAttr + `|` + styleAttr + styleUrl + `)`;
48
+ const cssPattern = `<style>[^<]*` + styleUrl;
49
+ const regex = new RegExp(
50
+ `(?<=(` + htmlPattern + `|` + cssPattern + `)\\s*?)${base}(?!\/)`,
51
+ 'gm'
52
+ );
53
+
54
+ const relativePath =
55
+ path
56
+ .relative(path.dirname(filePath), outDirPath)
57
+ .split(path.sep)
58
+ .join(path.posix.sep) || '.';
59
+
60
+ return html.replace(regex, `${relativePath}/`);
61
+ }
62
+
63
+ /**
64
+ * Replace absolute paths in CSS files with relative paths.
65
+ *
66
+ * @param {object} options
67
+ * @param {string} options.outDirPath - The path of the directory that `astro build` writes final build to.
68
+ * @param {string} options.filePath - The path of the target file.
69
+ * @param {string} options.base - The base path to deploy to.
70
+ * @param {string} options.css - The content of the CSS file.
71
+ * @returns {string} - Replaced CSS
72
+ */
73
+ function replaceCSS({
74
+ outDirPath,
75
+ filePath,
76
+ base,
77
+ css,
78
+ }: {
79
+ outDirPath: string;
80
+ filePath: string;
81
+ base: string;
82
+ css: string;
83
+ }) {
84
+ const { styleUrl } = pattern;
85
+ const regex = new RegExp(`(?<=` + styleUrl + `\\s*?)${base}(?!\/)`, 'gm');
86
+
87
+ const relativePath =
88
+ path
89
+ .relative(path.dirname(filePath), outDirPath)
90
+ .split(path.sep)
91
+ .join(path.posix.sep) || '.';
92
+
93
+ return css.replace(regex, `${relativePath}/`);
94
+ }
95
+
96
+ const getFilePaths = (() => {
97
+ async function get(dir: string, exts: string[], out: Record<string, string[]> = {}) {
98
+ const entries = await readdir(dir, { withFileTypes: true });
99
+ for (const entry of entries) {
100
+ const entryPath = path.join(dir, entry.name);
101
+ if (entry.isFile()) {
102
+ const fileExt = entryPath.slice(entryPath.lastIndexOf('.')).toLowerCase();
103
+ if (out[fileExt] === undefined) {
104
+ out[fileExt] = [];
105
+ }
106
+ if (exts.length > 0) {
107
+ for (const e of exts) {
108
+ if (e?.trim()?.toLowerCase() === fileExt) {
109
+ out[fileExt].push(entryPath);
110
+ break;
111
+ }
112
+ }
113
+ } else {
114
+ out[fileExt].push(entryPath);
115
+ }
116
+ } else if (entry.isDirectory()) {
117
+ await get(entryPath, exts, out);
118
+ }
119
+ }
120
+ }
121
+
122
+ return async (dir: string, ext: string | string[]): Promise<Record<string,string[]>> => {
123
+ const out: Record<string,string[]> = {};
124
+ await get(dir, (ext instanceof Array ? ext : [ext]), out);
125
+ return out;
126
+ };
127
+ })();
128
+
129
+ interface IntegrationProps {
130
+ enabled?: boolean;
131
+ }
132
+ export default function ({ enabled = true }: IntegrationProps): AstroIntegration {
133
+ let astroConfig: AstroConfig;
134
+
135
+ return {
136
+ name: 'relative-links',
137
+ hooks: {
138
+ 'astro:config:setup': ({ config }) => {
139
+ astroConfig = config;
140
+ },
141
+ 'astro:build:done': async ({ dir, logger }) => {
142
+ if (enabled) {
143
+ const base = leadingTrailingSlash(astroConfig.base);
144
+ logger.info('Making links relative');
145
+ // Use fileURLToPath to get a valid, cross-platform absolute path string
146
+ const outDirPath = fileURLToPath(dir);
147
+ const files = await getFilePaths(outDirPath, ['.css','.html']);
148
+ try {
149
+ // HTML
150
+ for (const filePath of files['.html']) {
151
+ const html = replaceHTML({
152
+ outDirPath,
153
+ filePath,
154
+ base,
155
+ html: await readFile(filePath, 'utf8'),
156
+ });
157
+ await writeFile(filePath, html, 'utf8');
158
+ }
159
+
160
+
161
+ // CSS
162
+ for (const filePath of files['.css']) {
163
+ const css = replaceCSS({
164
+ outDirPath,
165
+ filePath,
166
+ base,
167
+ css: await readFile(filePath, 'utf8'),
168
+ });
169
+ await writeFile(filePath, css, 'utf8');
170
+ }
171
+ } catch (error: any) {
172
+ logger.error(error.toString());
173
+ }
174
+ }
175
+ }
176
+ }
177
+ };
178
+ }