From fd72e89ba7785519c2b559e39aac6ccd6a12fd2c Mon Sep 17 00:00:00 2001 From: TootsCharlie Date: Mon, 3 Nov 2025 20:55:40 +0800 Subject: [PATCH] Add body option to gitalk configuration to support custom issue content (#877) Co-authored-by: Mimi <1119186082@qq.com> --- _config.yml | 4 ++++ source/js/third-party/comments/gitalk.js | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/_config.yml b/_config.yml index fd4516c..fd6318f 100644 --- a/_config.yml +++ b/_config.yml @@ -733,6 +733,10 @@ gitalk: # If you want everyone visiting your site to see a uniform language, you can set a force language value # Available values: en | es-ES | fr | ru | zh-CN | zh-TW language: + # If you want to customize the content of the initialized Issue, please edit the body parameter. + # For example: + # body: Comments for post ${title}, see ${url} + body: # Utterances # For more information: https://utteranc.es diff --git a/source/js/third-party/comments/gitalk.js b/source/js/third-party/comments/gitalk.js index 983d539..f91aceb 100644 --- a/source/js/third-party/comments/gitalk.js +++ b/source/js/third-party/comments/gitalk.js @@ -3,6 +3,20 @@ document.addEventListener('page:loaded', async () => { if (!CONFIG.page.comments) return; + // Parse and replace body expressions + const allowed = { + title: document.title, + url : location.href, + path : location.pathname, + lang : navigator.language + }; + + const parsedBody = CONFIG.gitalk.body?.replace(/\$\{([^}]+)}/g, (_, keyRaw) => { + const key = keyRaw.trim(); + if (!/^[A-Za-z0-9_]+$/.test(key)) return ''; + return String(allowed[key] ?? ''); + }); + await NexT.utils.loadComments('.gitalk-container'); await NexT.utils.getScript(CONFIG.gitalk.js, { condition: window.Gitalk @@ -16,7 +30,8 @@ document.addEventListener('page:loaded', async () => { id : CONFIG.gitalk.path_md5, proxy : CONFIG.gitalk.proxy, language : CONFIG.gitalk.language || window.navigator.language, - distractionFreeMode: CONFIG.gitalk.distraction_free_mode + distractionFreeMode: CONFIG.gitalk.distraction_free_mode, + body : parsedBody }); gitalk.render(document.querySelector('.gitalk-container')); });