Improve page scripts configuration files.

This commit is contained in:
凡梦星尘
2022-07-22 17:16:34 +08:00
parent a9ff14d312
commit 31f6e21671
15 changed files with 198 additions and 138 deletions

View File

@@ -1,92 +1,67 @@
if (!window.NexT) window.NexT = {};
(function() {
const className = 'next-config';
const siteConfig = {
"hostname" : "{{ .Site.BaseURL }}",
"root" : "/",
"vendor" : {
"plugins" : "{{ .Site.Params.vendors.plugins }}",
"router" : "{{ .Scratch.Get "router" }}"
},
"images" : "{{ .Site.Params.images }}",
"scheme" : "{{ .Site.Params.scheme }}",
"darkmode" : {{ .Site.Params.darkmode }},
"version" : "{{ .Site.Data.config.version }}",
"sidebar" : {{ .Site.Params.sidebar | jsonify }},
"copybtn" : {{ .Site.Params.codeblock.copyBtn }},
"bookmark" : {{ .Site.Params.bookmark | jsonify }},
"comments" : {{ .Site.Params.comments | jsonify }},
"lazyload" : {{ .Site.Params.lazyload }},
"pangu" : {{ .Site.Params.pangu }},
"stickytabs" : {{ .Site.Params.tabs.sticky }},
"motion" : {{ .Site.Params.motion | jsonify }},
// TODO Find prismjs
//"prism" : "",
"i18n" : {
"placeholder" : "",
"empty" : "${query}",
"hits_time" : "'${hits}', '${time}'",
"hits" : "${hits}"
},
{{- if .Site.Params.algoliaSearch.enable }}
// TODO
"algolia" : {
"appID" : "",
"apiKey" : "",
"indexName" : "",
"hits" : ""
const staticConfig = {};
let variableConfig = {};
const parse = text => JSON.parse(text || '{}');
const update = name => {
const targetEle = document.querySelector(`.${className}[data-name="${name}"]`);
if (!targetEle) return;
const parsedConfig = parse(targetEle.text);
if (name === 'main') {
Object.assign(staticConfig, parsedConfig);
} else {
variableConfig[name] = parsedConfig;
}
{{- end }}
{{- if .Site.Params.localSearch.enable }}
// TODO
"path" : "/search.json",
"localsearch": {{ .Site.Params.localSearch | jsonify }},
{{- end }}
{{- with .Site.Params.waline }}
"waline" : {
"js" : {{ $.Site.Data.resources.waline.js | jsonify }},
"css" : {{ $.Site.Data.resources.waline.css | jsonify }},
"config" : {{ . | jsonify }}
},
{{- end }}
{{- with .Site.Params.artalk }}
"artalk" : {
"js" : {{ $.Site.Data.resources.artalk.js | jsonify }},
"css" : {{ $.Site.Data.resources.artalk.css | jsonify }},
"config" : {{ . | jsonify }}
},
{{- end }}
{{- with .Site.Params.giscus }}
"giscus" : {
"js" : "{{ $.Site.Data.resources.giscus.js }}",
"config" : {{ . | jsonify }}
},
{{- end }}
{{- with .Site.Params.livere }}
"livere" : {
"js" : "{{ $.Site.Data.resources.livere.js }}"
},
{{- end }}
{{- with .Site.Params.utterances }}
"utterances" : {
"js" : "{{ $.Site.Data.resources.utterances.js }}",
"config" : {{ . | jsonify }}
},
{{- end }}
{{- with .Site.Params.addThisId }}
"addthis" : {
"js" : "{{ $.Site.Data.resources.addthis.js }}",
"config" : { "pubid" : "{{ . }}" }
},
{{- end }}
"lang" : "{{ .Site.LanguageCode }}",
"permalink" : "{{ .Page.Permalink | absURL }}",
"title" : "{{ .Page.Title }}",
"isHome" : {{ .IsHome }},
"isPage" : {{ .IsPage }}
};
window.NexT.CONFIG = new Proxy(siteConfig, {});
update('main');
window.NexT.CONFIG = new Proxy({}, {
get(overrideConfig, name) {
let existing;
if (name in staticConfig) {
existing = staticConfig[name];
} else {
if (!(name in variableConfig)) update(name);
existing = variableConfig[name];
}
// For unset override and mixable existing
if (!(name in overrideConfig) && typeof existing === 'object') {
// Get ready to mix.
overrideConfig[name] = {};
}
if (name in overrideConfig) {
const override = overrideConfig[name];
// When mixable
if (typeof override === 'object' && typeof existing === 'object') {
// Mix, proxy changes to the override.
return new Proxy({ ...existing, ...override }, {
set(target, prop, value) {
target[prop] = value;
override[prop] = value;
return true;
}
});
}
return override;
}
// Only when not mixable and override hasn't been set.
return existing;
}
});
// TODO
// document.addEventListener('pjax:success', () => {
// variableConfig = {};
// });
})();