Spaces:
Running
Running
thibaud frere commited on
Commit ·
58a7faa
1
Parent(s): 5cb0a27
add id props to the HtmlEmbed component
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647481 unmodified line5051525354555642 unmodified lines99100101102103104105---interface Props { src: string; title?: string; desc?: string; frameless?: boolean; align?: 'left' | 'center' | 'right' }const { src, title, desc, frameless = false, align = 'left' } = Astro.props as Props;// Load all .html embeds under src/content/embeds/** as strings (dev & build)const embeds = (import.meta as any).glob('../content/embeds/**/*.html', { query: '?raw', import: 'default', eager: true }) as Record<string, string>;function resolveFragment(requested: string): string | null {// Résolution tolérante: accepte avec/sans "embeds/", avec/sans ".html", insensible à la casse, et recherche par basenameconst norm = (s: string) => String(s || '').trim().replace(/^\/*/, '').replace(/^\.\//, '').toLowerCase();const raw = norm(requested);const withHtml = raw.endsWith('.html') ? raw : `${raw}.html`;const stripEmbeds = (s: string) => s.replace(/^embeds\//, '');const v1 = raw;const v2 = withHtml;const v3 = stripEmbeds(v1);const v4 = stripEmbeds(v2);const needles = new Set([v1, v2, v3, v4]);// Construire table des chemins normalisés -> contenuconst entries = Object.entries(embeds).map(([key, html]) => {const nk = norm(key);const base = nk.split('/').pop() || nk;return { nk, base, html };});// 1) Correspondance par fin de chemin complètefor (const e of entries) {for (const v of needles) {if (e.nk.endsWith('/' + v) || e.nk === v) return e.html;}}// 2) Correspondance par basenamefor (const e of entries) {for (const v of needles) {const vb = v.split('/').pop();if (e.base === vb) return e.html;}}return null;}1 unmodified lineconst mountId = `frag-${Math.random().toString(36).slice(2)}`;---{ html ? (<figure class="html-embed">{title && <figcaption class="html-embed__title" style={`text-align:${align}`}>{title}</figcaption>}<div class={`html-embed__card${frameless ? ' is-frameless' : ''}`}><div id={mountId} set:html={html} />42 unmodified lines</script><style is:global>.html-embed { margin: 0 0 var(--block-spacing-y); }.html-embed__title {text-align: left;font-weight: 600;1234567891011121314151617181 unmodified line2021222324252642 unmodified lines69707172737475---interface Props { src: string; title?: string; desc?: string; frameless?: boolean; align?: 'left' | 'center' | 'right'; id?: string }const { src, title, desc, frameless = false, align = 'left', id } = Astro.props as Props;// Load all .html embeds under src/content/embeds/** as strings (dev & build)const embeds = (import.meta as any).glob('../content/embeds/**/*.html', { query: '?raw', import: 'default', eager: true }) as Record<string, string>;function resolveFragment(requested: string): string | null {// Allow both "banner.html" and "embeds/banner.html"const needle = requested.replace(/^\/*/, '');for (const [key, html] of Object.entries(embeds)) {if (key.endsWith('/' + needle) || key.endsWith('/' + needle.replace(/^embeds\//, ''))) {return html;}}return null;}1 unmodified lineconst mountId = `frag-${Math.random().toString(36).slice(2)}`;---{ html ? (<figure class="html-embed" id={id}>{title && <figcaption class="html-embed__title" style={`text-align:${align}`}>{title}</figcaption>}<div class={`html-embed__card${frameless ? ' is-frameless' : ''}`}><div id={mountId} set:html={html} />42 unmodified lines</script><style is:global>.html-embed { margin: 0 0 var(--block-spacing-y); overflow: hidden; }.html-embed__title {text-align: left;font-weight: 600;
app/src/components/HtmlEmbed.astro+9-39