mirror of
https://github.com/Afilmory/afilmory
synced 2026-04-30 01:36:49 +00:00
- Added site configuration injection to the document for dynamic site settings. - Updated photo navigation links to include the correct path structure. - Introduced a new page for displaying individual photos with enhanced viewer functionality. - Refactored various components to utilize the new site configuration and improve code organization. Signed-off-by: Innei <tukon479@gmail.com>
41 lines
1.2 KiB
TypeScript
41 lines
1.2 KiB
TypeScript
import { DOMParser } from 'linkedom'
|
|
import type { Plugin } from 'vite'
|
|
|
|
import { siteConfig } from '../../../../site.config'
|
|
|
|
const CONFIG_SCRIPT_ID = 'config'
|
|
const INJECTED_SCRIPT_ID = 'config-runtime'
|
|
|
|
export function siteConfigInjectPlugin(): Plugin {
|
|
const siteConfigPayload = JSON.stringify(siteConfig)
|
|
const scriptContent = `window.__SITE_CONFIG__ = ${siteConfigPayload};`
|
|
const parser = new DOMParser()
|
|
|
|
return {
|
|
name: 'site-config-inject',
|
|
enforce: 'pre',
|
|
transformIndexHtml(html) {
|
|
const document = parser.parseFromString(html, 'text/html')
|
|
if (document.querySelector(`#${INJECTED_SCRIPT_ID}`)) {
|
|
return html
|
|
}
|
|
|
|
const scriptEl = document.createElement('script', 'text/javascript')
|
|
scriptEl.id = INJECTED_SCRIPT_ID
|
|
scriptEl.textContent = scriptContent
|
|
|
|
const configScript = document.querySelector(`#${CONFIG_SCRIPT_ID}`)
|
|
if (configScript?.parentNode) {
|
|
configScript.parentNode.insertBefore(scriptEl, configScript.nextSibling)
|
|
} else if (document.head) {
|
|
document.head.append(scriptEl)
|
|
} else {
|
|
const fallbackParent = document.body ?? document.documentElement
|
|
fallbackParent?.append(scriptEl)
|
|
}
|
|
|
|
return document.toString()
|
|
},
|
|
}
|
|
}
|