1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-06-13 19:22:13 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
Andrew
a4812e4a1f
Merge branch 'master' into bugfix-PR 2024-11-21 07:02:26 -05:00
Cotes Chung
65f960c31a
perf: speed up page rendering and jekyll build process (#2034)
Some checks failed
Close stale issues and PRs / stale (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Style Lint / stylelint (push) Has been cancelled
Lint Commit Messages / commitlint (push) Has been cancelled
Build and Deploy / build (push) Has been cancelled
Build and Deploy / deploy (push) Has been cancelled
- Ensure inline scripts execute after the DOM has fully loaded.
- Use Rollup to bundle the theme-mode and Mermaid scripts, reducing the number of Jekyll include snippets.
2024-11-16 22:49:55 +08:00
Cotes Chung
d51345e297
ci: reduce unnecessary pr-filter runs (#2033)
- Checking the repository of the PR is more effective than checking the label to identify bot-initiated PRs
- This change also allows more flexible PR body definitions for developers with write access to the repository
2024-11-08 22:35:18 +08:00
Cotes Chung
2f844978aa
chore: change stale label to inactive 2024-11-08 22:15:31 +08:00
Supreeth Mysore Venkatesh
42dea8ee29
build(deps): update wdm gem version for compatibility (#2028) 2024-11-04 00:45:59 +08:00
37 changed files with 414 additions and 411 deletions

View File

@ -6,6 +6,7 @@ on:
jobs: jobs:
check-template: check-template:
if: github.event.pull_request.head.repo.full_name != github.event.pull_request.base.repo.full_name
runs-on: ubuntu-latest runs-on: ubuntu-latest
permissions: permissions:
pull-requests: write pull-requests: write

View File

@ -16,8 +16,7 @@ module.exports = async ({ github, context, core }) => {
const action = context.payload.action; const action = context.payload.action;
const isValid = const isValid =
pr.labels.length > 0 || // PR create by Dependabot would have labels markdown !== '' && hasTypes(markdown) && hasDescription(markdown);
(markdown !== '' && hasTypes(markdown) && hasDescription(markdown));
if (!isValid) { if (!isValid) {
await github.rest.pulls.update({ await github.rest.pulls.update({

View File

@ -9,7 +9,7 @@ permissions:
pull-requests: write pull-requests: write
env: env:
STALE_LABEL: stale STALE_LABEL: inactive
EXEMPT_LABELS: "pending,planning,in progress" EXEMPT_LABELS: "pending,planning,in progress"
MESSAGE: > MESSAGE: >
This conversation has been automatically marked as stale because it has not had recent activity. This conversation has been automatically marked as stale because it has not had recent activity.

View File

@ -11,4 +11,4 @@ platforms :mingw, :x64_mingw, :mswin, :jruby do
gem "tzinfo-data" gem "tzinfo-data"
end end
gem "wdm", "~> 0.1.1", :platforms => [:mingw, :x64_mingw, :mswin] gem "wdm", "~> 0.2.0", :platforms => [:mingw, :x64_mingw, :mswin]

View File

@ -4,4 +4,3 @@
src="https://static.cloudflareinsights.com/beacon.min.js" src="https://static.cloudflareinsights.com/beacon.min.js"
data-cf-beacon='{"token": "{{ site.analytics.cloudflare.id }}"}' data-cf-beacon='{"token": "{{ site.analytics.cloudflare.id }}"}'
></script> ></script>
<!-- End Cloudflare Web Analytics -->

View File

@ -2,6 +2,5 @@
<script <script
src="https://cdn.usefathom.com/script.js" src="https://cdn.usefathom.com/script.js"
data-site="{{ site.analytics.fathom.id }}" data-site="{{ site.analytics.fathom.id }}"
defer> defer
</script> ></script>
<!-- End Fathom Code -->

View File

@ -1,7 +1,7 @@
<!-- Global site tag (gtag.js) - Google Analytics --> <!-- Global site tag (gtag.js) - Google Analytics -->
<script defer src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.id }}"></script> <script defer src="https://www.googletagmanager.com/gtag/js?id={{ site.analytics.google.id }}"></script>
<script> <script>
document.addEventListener('DOMContentLoaded', function (event) { document.addEventListener('DOMContentLoaded', () => {
window.dataLayer = window.dataLayer || []; window.dataLayer = window.dataLayer || [];
function gtag() { function gtag() {
dataLayer.push(arguments); dataLayer.push(arguments);

View File

@ -1,14 +1,13 @@
<!-- Matomo --> <!-- Matomo -->
<script type="text/javascript"> <script>
var _paq = window._paq = window._paq || []; document.addEventListener('DOMContentLoaded', () => {
_paq.push(['trackPageView']); var _paq = (window._paq = window._paq || []);
_paq.push(['enableLinkTracking']); _paq.push(['trackPageView']);
(function() { _paq.push(['enableLinkTracking']);
var u="//{{ site.analytics.matomo.domain }}/"; var u="//{{ site.analytics.matomo.domain }}/";
_paq.push(['setTrackerUrl', u+'matomo.php']); _paq.push(['setTrackerUrl', u+'matomo.php']);
_paq.push(['setSiteId', {{ site.analytics.matomo.id }}]); _paq.push(['setSiteId', {{ site.analytics.matomo.id }}]);
var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];
g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); g.type='text/javascript'; g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s);
})(); });
</script> </script>
<!-- End Matomo Code -->

View File

@ -1,38 +1,25 @@
<!-- The Disqus lazy loading. --> <script>
<div id="disqus_thread">
<p class="text-center text-muted small">Comments powered by <a href="https://disqus.com/">Disqus</a>.</p>
</div>
<script type="text/javascript">
var disqus_config = function () { var disqus_config = function () {
this.page.url = '{{ page.url | absolute_url }}'; this.page.url = '{{ page.url | absolute_url }}';
this.page.identifier = '{{ page.url }}'; this.page.identifier = '{{ page.url }}';
}; };
{%- comment -%} Lazy loading {%- endcomment -%} function addDisqus() {
var disqus_observer = new IntersectionObserver( let disqusThread = document.createElement('div');
function (entries) { let paragraph = document.createElement('p');
if (entries[0].isIntersecting) {
(function () {
var d = document,
s = d.createElement('script');
s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
})();
disqus_observer.disconnect(); disqusThread.id = 'disqus_thread';
} paragraph.className = 'text-center text-muted small';
}, paragraph.innerHTML = 'Comments powered by <a href="https://disqus.com/">Disqus</a>.';
{ threshold: [0] } disqusThread.appendChild(paragraph);
);
disqus_observer.observe(document.getElementById('disqus_thread')); const footer = document.querySelector('footer');
footer.insertAdjacentElement("beforebegin", disqusThread);
}
{%- comment -%} Auto switch theme {%- endcomment -%} {%- comment -%} Auto switch theme {%- endcomment -%}
function reloadDisqus() { function reloadDisqus(event) {
if (event.source === window && event.data && event.data.direction === ModeToggle.ID) { if (event.source === window && event.data && event.data.id === Theme.ID) {
{%- comment -%} Disqus hasn't been loaded {%- endcomment -%} {%- comment -%} Disqus hasn't been loaded {%- endcomment -%}
if (typeof DISQUS === 'undefined') { if (typeof DISQUS === 'undefined') {
return; return;
@ -44,7 +31,27 @@
} }
} }
if (document.getElementById('mode-toggle')) { addDisqus();
window.addEventListener('message', reloadDisqus);
if (Theme.switchable) {
addEventListener('message', reloadDisqus);
} }
{%- comment -%} Lazy loading {%- endcomment -%}
var disqusObserver = new IntersectionObserver(
function (entries) {
if (entries[0].isIntersecting) {
var d = document,
s = d.createElement('script');
s.src = 'https://{{ site.comments.disqus.shortname }}.disqus.com/embed.js';
s.setAttribute('data-timestamp', +new Date());
(d.head || d.body).appendChild(s);
disqusObserver.disconnect();
}
},
{ threshold: [0] }
);
disqusObserver.observe(document.getElementById('disqus_thread'));
</script> </script>

View File

@ -1,21 +1,8 @@
<!-- https://giscus.app/ --> <!-- https://giscus.app/ -->
<script type="text/javascript"> <script>
(function () { (function () {
const origin = 'https://giscus.app'; const themeMapper = Theme.getThemeMapper('light', 'dark_dimmed');
const lightTheme = 'light'; const initTheme = themeMapper[Theme.visualState];
const darkTheme = 'dark_dimmed';
let initTheme = lightTheme;
const html = document.documentElement;
if (
(html.hasAttribute('data-mode') &&
html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
initTheme = darkTheme;
}
let lang = '{{ site.comments.giscus.lang | default: lang }}'; let lang = '{{ site.comments.giscus.lang | default: lang }}';
{%- comment -%} https://github.com/giscus/giscus/tree/main/locales {%- endcomment -%} {%- comment -%} https://github.com/giscus/giscus/tree/main/locales {%- endcomment -%}
@ -41,30 +28,27 @@
async: '' async: ''
}; };
let giscusScript = document.createElement('script'); let giscusNode = document.createElement('script');
Object.entries(giscusAttributes).forEach(([key, value]) => Object.entries(giscusAttributes).forEach(([key, value]) =>
giscusScript.setAttribute(key, value) giscusNode.setAttribute(key, value)
); );
document.getElementById('tail-wrapper').appendChild(giscusScript);
const $footer = document.querySelector('footer');
$footer.insertAdjacentElement("beforebegin", giscusNode);
addEventListener('message', (event) => { addEventListener('message', (event) => {
if ( if (event.source === window && event.data && event.data.id === Theme.ID) {
event.source === window && const newTheme = themeMapper[Theme.visualState];
event.data &&
event.data.direction === ModeToggle.ID
) {
{%- comment -%} global theme mode changed {%- endcomment -%}
const mode = event.data.message;
const theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme;
const message = { const message = {
setConfig: { setConfig: {
theme: theme theme: newTheme
} }
}; };
const giscus = document.getElementsByClassName('giscus-frame')[0].contentWindow; const giscus =
giscus.postMessage({ giscus: message }, origin); document.getElementsByClassName('giscus-frame')[0].contentWindow;
giscus.postMessage({ giscus: message }, 'https://giscus.app');
} }
}); });
})(); })();

View File

@ -1,49 +1,38 @@
<!-- https://utteranc.es/ --> <!-- https://utteranc.es/ -->
<script <script>
src="https://utteranc.es/client.js"
repo="{{ site.comments.utterances.repo }}"
issue-term="{{ site.comments.utterances.issue_term }}"
crossorigin="anonymous"
async
></script>
<script type="text/javascript">
(function () { (function () {
const origin = 'https://utteranc.es'; const origin = 'https://utteranc.es';
const lightTheme = 'github-light'; const themeMapper = Theme.getThemeMapper('github-light', 'github-dark');
const darkTheme = 'github-dark'; const initTheme = themeMapper[Theme.visualState];
let initTheme = lightTheme;
const html = document.documentElement;
if ( let script = document.createElement('script');
(html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') || script.src = 'https://utteranc.es/client.js';
(!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches) script.setAttribute('repo', '{{ site.comments.utterances.repo }}');
) { script.setAttribute('issue-term', '{{ site.comments.utterances.issue_term }}');
initTheme = darkTheme; script.setAttribute('theme', initTheme);
} script.crossOrigin = 'anonymous';
script.async = true;
const $footer = document.querySelector('footer');
$footer.insertAdjacentElement('beforebegin', script);
addEventListener('message', (event) => { addEventListener('message', (event) => {
let theme; let newTheme;
{%- comment -%} credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347> {%- endcomment -%} {%- comment -%}
if (event.origin === origin) { Credit to <https://github.com/utterance/utterances/issues/170#issuecomment-594036347>
{%- comment -%} page initial {%- endcomment -%} {%- endcomment -%}
theme = initTheme; if (event.source === window && event.data && event.data.id === Theme.ID) {
} else if (event.source === window && event.data && event.data.direction === ModeToggle.ID) { newTheme = themeMapper[Theme.visualState];
{%- comment -%} global theme mode changed {%- endcomment -%}
const mode = event.data.message; const message = {
theme = mode === ModeToggle.DARK_MODE ? darkTheme : lightTheme; type: 'set-theme',
} else { theme: newTheme
return; };
const utterances = document.querySelector('.utterances-frame').contentWindow;
utterances.postMessage(message, origin);
} }
const message = {
type: 'set-theme',
theme: theme
};
const utterances = document.getElementsByClassName('utterances-frame')[0].contentWindow;
utterances.postMessage(message, origin);
}); });
})(); })();
</script> </script>

View File

@ -97,11 +97,32 @@
<link rel="stylesheet" href="{{ site.data.origin[type].glightbox.css | relative_url }}"> <link rel="stylesheet" href="{{ site.data.origin[type].glightbox.css | relative_url }}">
{% endif %} {% endif %}
<!-- JavaScript --> <!-- Scripts -->
{% unless site.theme_mode %} {% unless site.theme_mode %}
{% include mode-toggle.html %} <script src="{{ '/assets/js/dist/theme.min.js' | relative_url }}"></script>
{% endunless %} {% endunless %}
{% include js-selector.html lang=lang %}
{% if jekyll.environment == 'production' %}
<!-- PWA -->
{% if site.pwa.enabled %}
<script
defer
src="{{ '/app.min.js' | relative_url }}?baseurl={{ site.baseurl | default: '' }}&register={{ site.pwa.cache.enabled }}"
></script>
{% endif %}
<!-- Web Analytics -->
{% for analytics in site.analytics %}
{% capture str %}{{ analytics }}{% endcapture %}
{% assign platform = str | split: '{' | first %}
{% if site.analytics[platform].id and site.analytics[platform].id != empty %}
{% include analytics/{{ platform }}.html %}
{% endif %}
{% endfor %}
{% endif %}
{% include metadata-hook.html %} {% include metadata-hook.html %}
</head> </head>

View File

@ -62,12 +62,12 @@
{% capture script %}/assets/js/dist/{{ js }}.min.js{% endcapture %} {% capture script %}/assets/js/dist/{{ js }}.min.js{% endcapture %}
<script src="{{ script | relative_url }}"></script> <script defer src="{{ script | relative_url }}"></script>
{% if page.math %} {% if page.math %}
<!-- MathJax --> <!-- MathJax -->
<script src="{{ '/assets/js/data/mathjax.js' | relative_url }}"></script> <script async src="{{ '/assets/js/data/mathjax.js' | relative_url }}"></script>
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script> <script async src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script> <script id="MathJax-script" async src="{{ site.data.origin[type].mathjax.js | relative_url }}"></script>
{% endif %} {% endif %}
@ -84,26 +84,3 @@
{% endcase %} {% endcase %}
{% endif %} {% endif %}
{% endif %} {% endif %}
{% if page.mermaid %}
{% include mermaid.html %}
{% endif %}
{% if jekyll.environment == 'production' %}
<!-- PWA -->
{% if site.pwa.enabled %}
<script
defer
src="{{ 'app.min.js' | relative_url }}?baseurl={{ site.baseurl | default: '' }}&register={{ site.pwa.cache.enabled }}"
></script>
{% endif %}
<!-- Web Analytics -->
{% for analytics in site.analytics %}
{% capture str %}{{ analytics }}{% endcapture %}
{% assign type = str | split: '{' | first %}
{% if site.analytics[type].id and site.analytics[type].id != empty %}
{% include analytics/{{ type }}.html %}
{% endif %}
{% endfor %}
{% endif %}

View File

@ -1,6 +1,6 @@
{% assign urls = include.urls | split: ',' %} {% assign urls = include.urls | split: ',' %}
{% assign combined_urls = nil %} {% assign combined_urls = null %}
{% assign domain = 'https://cdn.jsdelivr.net/' %} {% assign domain = 'https://cdn.jsdelivr.net/' %}
@ -15,12 +15,12 @@
{% endif %} {% endif %}
{% elsif url contains '//' %} {% elsif url contains '//' %}
<script src="{{ url }}"></script> <script defer src="{{ url }}"></script>
{% else %} {% else %}
<script src="{{ url | relative_url }}"></script> <script defer src="{{ url | relative_url }}"></script>
{% endif %} {% endif %}
{% endfor %} {% endfor %}
{% if combined_urls %} {% if combined_urls %}
<script src="{{ combined_urls }}"></script> <script defer src="{{ combined_urls }}"></script>
{% endif %} {% endif %}

View File

@ -1,62 +0,0 @@
<!-- mermaid-js loader -->
<script type="text/javascript">
function updateMermaid(event) {
if (event.source === window && event.data && event.data.direction === ModeToggle.ID) {
const mode = event.data.message;
if (typeof mermaid === 'undefined') {
return;
}
let expectedTheme = mode === ModeToggle.DARK_MODE ? 'dark' : 'default';
let config = { theme: expectedTheme };
{%- comment -%}
Re-render the SVG <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344>
{%- endcomment -%}
const mermaidList = document.getElementsByClassName('mermaid');
[...mermaidList].forEach((elem) => {
const svgCode = elem.previousSibling.children.item(0).innerHTML;
elem.innerHTML = svgCode;
elem.removeAttribute('data-processed');
});
mermaid.initialize(config);
mermaid.init(undefined, '.mermaid');
}
}
(function () {
let initTheme = 'default';
const html = document.documentElement;
if (
(html.hasAttribute('data-mode') && html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') && window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
initTheme = 'dark';
}
let mermaidConf = {
theme: initTheme {%- comment -%} <default | dark | forest | neutral> {%- endcomment -%}
};
{%- comment -%} Create mermaid tag {%- endcomment -%}
const basicList = document.getElementsByClassName('language-mermaid');
[...basicList].forEach((elem) => {
const svgCode = elem.textContent;
const backup = elem.parentElement;
backup.classList.add('d-none');
{%- comment -%} create mermaid node {%- endcomment -%}
let mermaid = document.createElement('pre');
mermaid.classList.add('mermaid');
const text = document.createTextNode(svgCode);
mermaid.appendChild(text);
backup.after(mermaid);
});
mermaid.initialize(mermaidConf);
window.addEventListener('message', updateMermaid);
})();
</script>

View File

@ -1,116 +0,0 @@
<!-- Switch the mode between dark and light. -->
<script type="text/javascript">
class ModeToggle {
static get MODE_KEY() {
return 'mode';
}
static get MODE_ATTR() {
return 'data-mode';
}
static get DARK_MODE() {
return 'dark';
}
static get LIGHT_MODE() {
return 'light';
}
static get ID() {
return 'mode-toggle';
}
constructor() {
let self = this;
{%- comment -%} always follow the system prefers {%- endcomment -%}
this.sysDarkPrefers.addEventListener('change', () => {
if (self.hasMode) {
self.clearMode();
}
self.notify();
});
if (!this.hasMode) {
return;
}
if (this.isDarkMode) {
this.setDark();
} else {
this.setLight();
}
}
get sysDarkPrefers() {
return window.matchMedia('(prefers-color-scheme: dark)');
}
get isPreferDark() {
return this.sysDarkPrefers.matches;
}
get isDarkMode() {
return this.mode === ModeToggle.DARK_MODE;
}
get hasMode() {
return this.mode != null;
}
get mode() {
return sessionStorage.getItem(ModeToggle.MODE_KEY);
}
{%- comment -%} get the current mode on screen {%- endcomment -%}
get modeStatus() {
if (this.hasMode) {
return this.mode;
} else {
return this.isPreferDark ? ModeToggle.DARK_MODE : ModeToggle.LIGHT_MODE;
}
}
setDark() {
document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.DARK_MODE);
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.DARK_MODE);
}
setLight() {
document.documentElement.setAttribute(ModeToggle.MODE_ATTR, ModeToggle.LIGHT_MODE);
sessionStorage.setItem(ModeToggle.MODE_KEY, ModeToggle.LIGHT_MODE);
}
clearMode() {
document.documentElement.removeAttribute(ModeToggle.MODE_ATTR);
sessionStorage.removeItem(ModeToggle.MODE_KEY);
}
{%- comment -%}
Notify another plugins that the theme mode has changed
{%- endcomment -%}
notify() {
window.postMessage(
{
direction: ModeToggle.ID,
message: this.modeStatus
},
'*'
);
}
flipMode() {
if (this.hasMode) {
this.clearMode();
} else {
if (this.isPreferDark) {
this.setLight();
} else {
this.setDark();
}
}
this.notify();
}
}
const modeToggle = new ModeToggle();
</script>

View File

@ -19,29 +19,31 @@
{% capture not_found %}<p class="mt-5">{{ site.data.locales[include.lang].search.no_results }}</p>{% endcapture %} {% capture not_found %}<p class="mt-5">{{ site.data.locales[include.lang].search.no_results }}</p>{% endcapture %}
<script> <script>
{%- comment -%} Note: dependent library will be loaded in `js-selector.html` {%- endcomment -%} {% comment %} Note: dependent library will be loaded in `js-selector.html` {% endcomment %}
SimpleJekyllSearch({ document.addEventListener('DOMContentLoaded', () => {
searchInput: document.getElementById('search-input'), SimpleJekyllSearch({
resultsContainer: document.getElementById('search-results'), searchInput: document.getElementById('search-input'),
json: '{{ '/assets/js/data/search.json' | relative_url }}', resultsContainer: document.getElementById('search-results'),
searchResultTemplate: '{{ result_elem | strip_newlines }}', json: '{{ '/assets/js/data/search.json' | relative_url }}',
noResultsText: '{{ not_found }}', searchResultTemplate: '{{ result_elem | strip_newlines }}',
templateMiddleware: function(prop, value, template) { noResultsText: '{{ not_found }}',
if (prop === 'categories') { templateMiddleware: function(prop, value, template) {
if (value === '') { if (prop === 'categories') {
return `${value}`; if (value === '') {
} else { return `${value}`;
return `<div class="me-sm-4"><i class="far fa-folder fa-fw"></i>${value}</div>`; } else {
return `<div class="me-sm-4"><i class="far fa-folder fa-fw"></i>${value}</div>`;
}
} }
}
if (prop === 'tags') { if (prop === 'tags') {
if (value === '') { if (value === '') {
return `${value}`; return `${value}`;
} else { } else {
return `<div><i class="fa fa-tag fa-fw"></i>${value}</div>`; return `<div><i class="fa fa-tag fa-fw"></i>${value}</div>`;
}
} }
} }
} });
}); });
</script> </script>

View File

@ -1,5 +1,5 @@
import { basic, initSidebar, initTopbar } from './modules/layouts'; import { basic, initSidebar, initTopbar } from './modules/layouts';
import { categoryCollapse } from './modules/plugins'; import { categoryCollapse } from './modules/components';
basic(); basic();
initSidebar(); initSidebar();

View File

@ -1,5 +1,5 @@
import { basic, initSidebar, initTopbar } from './modules/layouts'; import { basic, initSidebar, initTopbar } from './modules/layouts';
import { initLocaleDatetime, loadImg } from './modules/plugins'; import { initLocaleDatetime, loadImg } from './modules/components';
loadImg(); loadImg();
initLocaleDatetime(); initLocaleDatetime();

View File

@ -1,5 +1,5 @@
import { basic, initSidebar, initTopbar } from './modules/layouts'; import { basic, initSidebar, initTopbar } from './modules/layouts';
import { initLocaleDatetime } from './modules/plugins'; import { initLocaleDatetime } from './modules/components';
initSidebar(); initSidebar();
initTopbar(); initTopbar();

View File

@ -4,3 +4,7 @@ export { loadImg } from './components/img-loading';
export { imgPopup } from './components/img-popup'; export { imgPopup } from './components/img-popup';
export { initLocaleDatetime } from './components/locale-datetime'; export { initLocaleDatetime } from './components/locale-datetime';
export { initToc } from './components/toc'; export { initToc } from './components/toc';
export { loadMermaid } from './components/mermaid';
export { modeWatcher } from './components/mode-toggle';
export { back2top } from './components/back-to-top';
export { loadTooptip } from './components/tooltip-loader';

View File

@ -4,7 +4,6 @@
* Dependencies: https://github.com/biati-digital/glightbox * Dependencies: https://github.com/biati-digital/glightbox
*/ */
const html = document.documentElement;
const lightImages = '.popup:not(.dark)'; const lightImages = '.popup:not(.dark)';
const darkImages = '.popup:not(.light)'; const darkImages = '.popup:not(.light)';
let selector = lightImages; let selector = lightImages;
@ -33,26 +32,17 @@ export function imgPopup() {
document.querySelector('.popup.dark') === null document.querySelector('.popup.dark') === null
); );
if ( if (Theme.visualState === Theme.DARK) {
(html.hasAttribute('data-mode') &&
html.getAttribute('data-mode') === 'dark') ||
(!html.hasAttribute('data-mode') &&
window.matchMedia('(prefers-color-scheme: dark)').matches)
) {
selector = darkImages; selector = darkImages;
} }
let current = GLightbox({ selector: `${selector}` }); let current = GLightbox({ selector: `${selector}` });
if (hasDualImages && document.getElementById('mode-toggle')) { if (hasDualImages && Theme.switchable) {
let reverse = null; let reverse = null;
window.addEventListener('message', (event) => { window.addEventListener('message', (event) => {
if ( if (event.source === window && event.data && event.data.id === Theme.ID) {
event.source === window &&
event.data &&
event.data.direction === ModeToggle.ID
) {
updateImages(current, reverse); updateImages(current, reverse);
} }
}); });

View File

@ -0,0 +1,60 @@
/**
* Mermaid-js loader
*/
const MERMAID = 'mermaid';
const themeMapper = Theme.getThemeMapper('default', 'dark');
function refreshTheme(event) {
if (event.source === window && event.data && event.data.id === Theme.ID) {
// Re-render the SVG <https://github.com/mermaid-js/mermaid/issues/311#issuecomment-332557344>
const mermaidList = document.getElementsByClassName(MERMAID);
[...mermaidList].forEach((elem) => {
const svgCode = elem.previousSibling.children.item(0).innerHTML;
elem.textContent = svgCode;
elem.removeAttribute('data-processed');
});
const newTheme = themeMapper[Theme.visualState];
mermaid.initialize({ theme: newTheme });
mermaid.init(null, `.${MERMAID}`);
}
}
function setNode(elem) {
const svgCode = elem.textContent;
const backup = elem.parentElement;
backup.classList.add('d-none');
// Create mermaid node
const mermaid = document.createElement('pre');
mermaid.classList.add(MERMAID);
const text = document.createTextNode(svgCode);
mermaid.appendChild(text);
backup.after(mermaid);
}
export function loadMermaid() {
if (
typeof mermaid === 'undefined' ||
typeof mermaid.initialize !== 'function'
) {
return;
}
const initTheme = themeMapper[Theme.visualState];
let mermaidConf = {
theme: initTheme
};
const basicList = document.getElementsByClassName('language-mermaid');
[...basicList].forEach(setNode);
mermaid.initialize(mermaidConf);
if (Theme.switchable) {
window.addEventListener('message', refreshTheme);
}
}

View File

@ -0,0 +1,15 @@
/**
* Add listener for theme mode toggle
*/
const $toggle = document.getElementById('mode-toggle');
export function modeWatcher() {
if (!$toggle) {
return;
}
$toggle.addEventListener('click', () => {
Theme.flip();
});
}

View File

@ -1,14 +0,0 @@
/**
* Add listener for theme mode toggle
*/
const toggle = document.getElementById('mode-toggle');
export function modeWatcher() {
if (!toggle) {
return;
}
toggle.addEventListener('click', () => {
modeToggle.flipMode();
});
}

View File

@ -1,22 +0,0 @@
/**
* Expand or close the sidebar in mobile screens.
*/
const $sidebar = document.getElementById('sidebar');
const $trigger = document.getElementById('sidebar-trigger');
const $mask = document.getElementById('mask');
class SidebarUtil {
static #isExpanded = false;
static toggle() {
this.#isExpanded = !this.#isExpanded;
document.body.toggleAttribute('sidebar-display', this.#isExpanded);
$sidebar.classList.toggle('z-2', this.#isExpanded);
$mask.classList.toggle('d-none', !this.#isExpanded);
}
}
export function sidebarExpand() {
$trigger.onclick = $mask.onclick = () => SidebarUtil.toggle();
}

View File

@ -1,7 +1,7 @@
import { back2top } from '../components/back-to-top'; import { back2top, loadTooptip, modeWatcher } from '../components';
import { loadTooptip } from '../components/tooltip-loader';
export function basic() { export function basic() {
modeWatcher();
back2top(); back2top();
loadTooptip(); loadTooptip();
} }

View File

@ -1,7 +1,19 @@
import { modeWatcher } from '../components/mode-watcher'; const ATTR_DISPLAY = 'sidebar-display';
import { sidebarExpand } from '../components/sidebar'; const $sidebar = document.getElementById('sidebar');
const $trigger = document.getElementById('sidebar-trigger');
const $mask = document.getElementById('mask');
class SidebarUtil {
static #isExpanded = false;
static toggle() {
this.#isExpanded = !this.#isExpanded;
document.body.toggleAttribute(ATTR_DISPLAY, this.#isExpanded);
$sidebar.classList.toggle('z-2', this.#isExpanded);
$mask.classList.toggle('d-none', !this.#isExpanded);
}
}
export function initSidebar() { export function initSidebar() {
modeWatcher(); $trigger.onclick = $mask.onclick = () => SidebarUtil.toggle();
sidebarExpand();
} }

View File

@ -0,0 +1,135 @@
/**
* Theme management class
*
* To reduce flickering during page load, this script should be loaded synchronously.
*/
class Theme {
static #modeKey = 'mode';
static #modeAttr = 'data-mode';
static #darkMedia = window.matchMedia('(prefers-color-scheme: dark)');
static switchable = !document.documentElement.hasAttribute(this.#modeAttr);
static get DARK() {
return 'dark';
}
static get LIGHT() {
return 'light';
}
/**
* @returns {string} Theme mode identifier
*/
static get ID() {
return 'theme-mode';
}
/**
* Gets the current visual state of the theme.
*
* @returns {string} The current visual state, either the mode if it exists,
* or the system dark mode state ('dark' or 'light').
*/
static get visualState() {
if (this.#hasMode) {
return this.#mode;
} else {
return this.#sysDark ? this.DARK : this.LIGHT;
}
}
static get #mode() {
return sessionStorage.getItem(this.#modeKey);
}
static get #isDarkMode() {
return this.#mode === this.DARK;
}
static get #hasMode() {
return this.#mode !== null;
}
static get #sysDark() {
return this.#darkMedia.matches;
}
/**
* Maps theme modes to provided values
* @param {string} light Value for light mode
* @param {string} dark Value for dark mode
* @returns {Object} Mapped values
*/
static getThemeMapper(light, dark) {
return {
[this.LIGHT]: light,
[this.DARK]: dark
};
}
/**
* Initializes the theme based on system preferences or stored mode
*/
static init() {
if (!this.switchable) {
return;
}
this.#darkMedia.addEventListener('change', () => {
const lastMode = this.#mode;
this.#clearMode();
if (lastMode !== this.visualState) {
this.#notify();
}
});
if (!this.#hasMode) {
return;
}
if (this.#isDarkMode) {
this.#setDark();
} else {
this.#setLight();
}
}
/**
* Flips the current theme mode
*/
static flip() {
if (this.#hasMode) {
this.#clearMode();
} else {
this.#sysDark ? this.#setLight() : this.#setDark();
}
this.#notify();
}
static #setDark() {
document.documentElement.setAttribute(this.#modeAttr, this.DARK);
sessionStorage.setItem(this.#modeKey, this.DARK);
}
static #setLight() {
document.documentElement.setAttribute(this.#modeAttr, this.LIGHT);
sessionStorage.setItem(this.#modeKey, this.LIGHT);
}
static #clearMode() {
document.documentElement.removeAttribute(this.#modeAttr);
sessionStorage.removeItem(this.#modeKey);
}
/**
* Notifies other plugins that the theme mode has changed
*/
static #notify() {
window.postMessage({ id: this.ID }, '*');
}
}
Theme.init();
export default Theme;

View File

@ -1,9 +1,15 @@
import { basic, initSidebar, initTopbar } from './modules/layouts'; import { basic, initSidebar, initTopbar } from './modules/layouts';
import { loadImg, imgPopup, initClipboard } from './modules/plugins'; import {
loadImg,
imgPopup,
initClipboard,
loadMermaid
} from './modules/components';
loadImg(); loadImg();
imgPopup(); imgPopup();
initSidebar(); initSidebar();
initTopbar(); initTopbar();
initClipboard(); initClipboard();
loadMermaid();
basic(); basic();

View File

@ -5,8 +5,9 @@ import {
imgPopup, imgPopup,
initLocaleDatetime, initLocaleDatetime,
initClipboard, initClipboard,
initToc initToc,
} from './modules/plugins'; loadMermaid
} from './modules/components';
loadImg(); loadImg();
initToc(); initToc();
@ -15,4 +16,5 @@ initSidebar();
initLocaleDatetime(); initLocaleDatetime();
initClipboard(); initClipboard();
initTopbar(); initTopbar();
loadMermaid();
basic(); basic();

View File

@ -74,8 +74,12 @@ layout: compress
{% include_cached notification.html lang=lang %} {% include_cached notification.html lang=lang %}
{% endif %} {% endif %}
<!-- JavaScripts --> <!-- Embedded scripts -->
{% include js-selector.html lang=lang %}
{% for _include in layout.script_includes %}
{% assign _include_path = _include | append: '.html' %}
{% include {{ _include_path }} %}
{% endfor %}
{% include_cached search-loader.html lang=lang %} {% include_cached search-loader.html lang=lang %}
</body> </body>

View File

@ -6,7 +6,8 @@ panel_includes:
tail_includes: tail_includes:
- related-posts - related-posts
- post-nav - post-nav
- comments script_includes:
- comment
--- ---
{% include lang.html %} {% include lang.html %}

View File

@ -535,6 +535,7 @@ header {
.utterances { .utterances {
max-width: 100%; max-width: 100%;
min-height: 269px;
} }
%btn-share-hover { %btn-share-hover {

View File

@ -30,6 +30,7 @@
"devDependencies": { "devDependencies": {
"@babel/core": "^7.25.2", "@babel/core": "^7.25.2",
"@babel/plugin-transform-class-properties": "^7.25.4", "@babel/plugin-transform-class-properties": "^7.25.4",
"@babel/plugin-transform-private-methods": "^7.25.7",
"@babel/preset-env": "^7.25.4", "@babel/preset-env": "^7.25.4",
"@commitlint/cli": "^19.5.0", "@commitlint/cli": "^19.5.0",
"@commitlint/config-conventional": "^19.5.0", "@commitlint/config-conventional": "^19.5.0",

View File

@ -34,24 +34,32 @@ function insertFrontmatter() {
}; };
} }
function build(filename, { src = SRC_DEFAULT, jekyll = false } = {}) { function build(
filename,
{ src = SRC_DEFAULT, jekyll = false, outputName = null } = {}
) {
const input = `${src}/${filename}.js`;
return { return {
input: `${src}/${filename}.js`, input,
output: { output: {
file: `${DIST}/${filename}.min.js`, file: `${DIST}/${filename}.min.js`,
format: 'iife', format: 'iife',
name: 'Chirpy', ...(outputName !== null && { name: outputName }),
banner, banner,
sourcemap: !isProd && !jekyll sourcemap: !isProd && !jekyll
}, },
watch: { watch: {
include: `${src}/**` include: input
}, },
plugins: [ plugins: [
babel({ babel({
babelHelpers: 'bundled', babelHelpers: 'bundled',
presets: ['@babel/env'], presets: ['@babel/env'],
plugins: ['@babel/plugin-transform-class-properties'] plugins: [
'@babel/plugin-transform-class-properties',
'@babel/plugin-transform-private-methods'
]
}), }),
nodeResolve(), nodeResolve(),
isProd && terser(), isProd && terser(),
@ -69,6 +77,7 @@ export default [
build('page'), build('page'),
build('post'), build('post'),
build('misc'), build('misc'),
build('theme', { src: `${SRC_DEFAULT}/modules`, outputName: 'Theme' }),
build('app', { src: SRC_PWA, jekyll: true }), build('app', { src: SRC_PWA, jekyll: true }),
build('sw', { src: SRC_PWA, jekyll: true }) build('sw', { src: SRC_PWA, jekyll: true })
]; ];