| --- |
| interface Props { src: string } |
| const { src } = Astro.props as Props; |
|
|
| |
| const fragments = import.meta.glob('../content/fragments/**/*.html', { query: '?raw', import: 'default', eager: true }) as Record<string, string>; |
|
|
| function resolveFragment(requested: string): string | null { |
| |
| const needle = requested.replace(/^\/*/, ''); |
| for (const [key, html] of Object.entries(fragments)) { |
| if (key.endsWith('/' + needle) || key.endsWith('/' + needle.replace(/^fragments\//, ''))) { |
| return html; |
| } |
| } |
| return null; |
| } |
|
|
| const html = resolveFragment(src); |
| const mountId = `frag-${Math.random().toString(36).slice(2)}`; |
| --- |
| { html ? ( |
| <div id={mountId} set:html={html} /> |
| ) : ( |
| <div></div> |
| ) } |
|
|
| <script> |
| |
| const scriptEl = document.currentScript; |
| const mount = scriptEl ? scriptEl.previousElementSibling : null; |
| const execute = () => { |
| if (!mount) return; |
| const scripts = mount.querySelectorAll('script'); |
| scripts.forEach(old => { |
| |
| if (old.type && old.type !== 'text/javascript' && old.type !== 'module' && old.type !== '') return; |
| if (old.dataset.executed === 'true') return; |
| old.dataset.executed = 'true'; |
| if (old.src) { |
| const s = document.createElement('script'); |
| Array.from(old.attributes).forEach(({ name, value }) => s.setAttribute(name, value)); |
| document.body.appendChild(s); |
| } else { |
| try { |
| |
| (0, eval)(old.text || ''); |
| } catch (e) { |
| console.error('HtmlFragment inline script error:', e); |
| } |
| } |
| }); |
| }; |
| |
| if (window.Plotly || window.d3 || document.readyState === 'complete') execute(); |
| else window.addEventListener('load', execute, { once: true }); |
| </script> |
|
|
|
|
|
|