Spaces:
Running
Running
thibaud frere
commited on
Commit
·
58a7faa
1
Parent(s):
5cb0a27
add id props to the HtmlEmbed component
Browse files
app/src/components/HtmlEmbed.astro
CHANGED
@@ -1,48 +1,18 @@
|
|
1 |
---
|
2 |
-
interface Props { src: string; title?: string; desc?: string; frameless?: boolean; align?: 'left' | 'center' | 'right' }
|
3 |
-
const { src, title, desc, frameless = false, align = 'left' } = Astro.props as Props;
|
4 |
|
5 |
// Load all .html embeds under src/content/embeds/** as strings (dev & build)
|
6 |
const embeds = (import.meta as any).glob('../content/embeds/**/*.html', { query: '?raw', import: 'default', eager: true }) as Record<string, string>;
|
7 |
|
8 |
function resolveFragment(requested: string): string | null {
|
9 |
-
//
|
10 |
-
const
|
11 |
-
|
12 |
-
.replace(
|
13 |
-
|
14 |
-
.toLowerCase();
|
15 |
-
const raw = norm(requested);
|
16 |
-
const withHtml = raw.endsWith('.html') ? raw : `${raw}.html`;
|
17 |
-
const stripEmbeds = (s: string) => s.replace(/^embeds\//, '');
|
18 |
-
const v1 = raw;
|
19 |
-
const v2 = withHtml;
|
20 |
-
const v3 = stripEmbeds(v1);
|
21 |
-
const v4 = stripEmbeds(v2);
|
22 |
-
const needles = new Set([v1, v2, v3, v4]);
|
23 |
-
|
24 |
-
// Construire table des chemins normalisés -> contenu
|
25 |
-
const entries = Object.entries(embeds).map(([key, html]) => {
|
26 |
-
const nk = norm(key);
|
27 |
-
const base = nk.split('/').pop() || nk;
|
28 |
-
return { nk, base, html };
|
29 |
-
});
|
30 |
-
|
31 |
-
// 1) Correspondance par fin de chemin complète
|
32 |
-
for (const e of entries) {
|
33 |
-
for (const v of needles) {
|
34 |
-
if (e.nk.endsWith('/' + v) || e.nk === v) return e.html;
|
35 |
-
}
|
36 |
-
}
|
37 |
-
|
38 |
-
// 2) Correspondance par basename
|
39 |
-
for (const e of entries) {
|
40 |
-
for (const v of needles) {
|
41 |
-
const vb = v.split('/').pop();
|
42 |
-
if (e.base === vb) return e.html;
|
43 |
}
|
44 |
}
|
45 |
-
|
46 |
return null;
|
47 |
}
|
48 |
|
@@ -50,7 +20,7 @@ const html = resolveFragment(src);
|
|
50 |
const mountId = `frag-${Math.random().toString(36).slice(2)}`;
|
51 |
---
|
52 |
{ html ? (
|
53 |
-
<figure class="html-embed">
|
54 |
{title && <figcaption class="html-embed__title" style={`text-align:${align}`}>{title}</figcaption>}
|
55 |
<div class={`html-embed__card${frameless ? ' is-frameless' : ''}`}>
|
56 |
<div id={mountId} set:html={html} />
|
@@ -99,7 +69,7 @@ const mountId = `frag-${Math.random().toString(36).slice(2)}`;
|
|
99 |
</script>
|
100 |
|
101 |
<style is:global>
|
102 |
-
.html-embed { margin: 0 0 var(--block-spacing-y); }
|
103 |
.html-embed__title {
|
104 |
text-align: left;
|
105 |
font-weight: 600;
|
|
|
1 |
---
|
2 |
+
interface Props { src: string; title?: string; desc?: string; frameless?: boolean; align?: 'left' | 'center' | 'right'; id?: string }
|
3 |
+
const { src, title, desc, frameless = false, align = 'left', id } = Astro.props as Props;
|
4 |
|
5 |
// Load all .html embeds under src/content/embeds/** as strings (dev & build)
|
6 |
const embeds = (import.meta as any).glob('../content/embeds/**/*.html', { query: '?raw', import: 'default', eager: true }) as Record<string, string>;
|
7 |
|
8 |
function resolveFragment(requested: string): string | null {
|
9 |
+
// Allow both "banner.html" and "embeds/banner.html"
|
10 |
+
const needle = requested.replace(/^\/*/, '');
|
11 |
+
for (const [key, html] of Object.entries(embeds)) {
|
12 |
+
if (key.endsWith('/' + needle) || key.endsWith('/' + needle.replace(/^embeds\//, ''))) {
|
13 |
+
return html;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
}
|
15 |
}
|
|
|
16 |
return null;
|
17 |
}
|
18 |
|
|
|
20 |
const mountId = `frag-${Math.random().toString(36).slice(2)}`;
|
21 |
---
|
22 |
{ html ? (
|
23 |
+
<figure class="html-embed" id={id}>
|
24 |
{title && <figcaption class="html-embed__title" style={`text-align:${align}`}>{title}</figcaption>}
|
25 |
<div class={`html-embed__card${frameless ? ' is-frameless' : ''}`}>
|
26 |
<div id={mountId} set:html={html} />
|
|
|
69 |
</script>
|
70 |
|
71 |
<style is:global>
|
72 |
+
.html-embed { margin: 0 0 var(--block-spacing-y); overflow: hidden; }
|
73 |
.html-embed__title {
|
74 |
text-align: left;
|
75 |
font-weight: 600;
|