fix: plugins: custom.html was not working, exception made for sections top,bottom,style,htmlHead

Massimo Melina committed Jun 5, 2024 at 16:12 UTC 17ff49481ecd4e397077d6757e416a79fa17cd73
3 files changed +12 -5
src/customHtml.ts
+9 -1
@@ -4,6 +4,7 @@ import { proxy } from 'valtio/vanilla' // without /vanilla we trigger react depe
4 import Dict = NodeJS.Dict
5 import { writeFile } from 'fs/promises'
6 import { mapPlugins } from './plugins'
7 +import _ from 'lodash'
8
9 const FILE = 'custom.html'
10
@@ -35,7 +36,14 @@ export function watchLoadCustomHtml(folder='') {
36
37 export function getSection(name: string) {
38 return (customHtmlState.sections.get(name) || '')
38 - + mapPlugins(pl => pl.getData().customHtml?.get(name)).join('\n')
39 + + mapPlugins(pl => pl.getData().customHtml?.()[name]).join('\n')
40 +}
41 +
42 +export function getAllSections() {
43 + const keys = mapPlugins(pl => Object.keys(pl.getData().customHtml?.()))
44 + keys.push(Array.from(customHtmlState.sections.keys()))
45 + const all = _.uniq(keys.flat())
46 + return Object.fromEntries(all.map(x => [x, getSection(x)]))
47 }
48
49 export async function saveCustomHtml(sections: Dict<string>) {
src/plugins.ts
+1 -1
@@ -451,7 +451,7 @@ function watchPlugin(id: string, path: string) {
451 })
452 const folder = dirname(module)
453 const { state, unwatch } = watchLoadCustomHtml(folder)
454 - pluginData.customHtml = state
454 + pluginData.customHtml ??= () => Object.fromEntries(state)
455
456 const plugin = new Plugin(id, folder, pluginData, async () => {
457 unwatch()
src/serveGuiFiles.ts
+2 -3
@@ -12,7 +12,7 @@ import { join, extname } from 'path'
12 import { CFG, debounceAsync, FRONTEND_OPTIONS, newObj, onlyTruthy, parseFile } from './misc'
13 import { favicon, title } from './adminApis'
14 import { subscribe } from 'valtio/vanilla'
15 -import { customHtmlState, getSection } from './customHtml'
15 +import { customHtmlState, getAllSections, getSection } from './customHtml'
16 import _ from 'lodash'
17 import { defineConfig, getConfig } from './config'
18 import { getLangData } from './lang'
@@ -111,8 +111,7 @@ async function treatIndex(ctx: Koa.Context, filesUri: string, body: string) {
111 prefixUrl: ctx.state.revProxyPath,
112 dontOverwriteUploading: dontOverwriteUploading.get(),
113 forceTheme: mapPlugins(p => _.isString(p.isTheme) ? p.isTheme : undefined).find(Boolean),
114 - customHtml: _.omit(Object.fromEntries(customHtmlState.sections),
115 - ['top', 'bottom']), // exclude the sections we already apply in this phase
114 + customHtml: _.omit(getAllSections(), ['top', 'bottom', 'htmlHead', 'style']), // exclude the sections we already apply in this phase
115 ...newObj(FRONTEND_OPTIONS, (v, k) => getConfig(k)),
116 lang
117 }, null, 4).replace(/<(\/script)/g, '<"+"$1') /*avoid breaking our script container*/}