|
| 1 | +/** |
| 2 | + * @typedef {import('unist').Node} Node |
| 3 | + * @typedef {import('vfile').VFile} VFile |
| 4 | + */ |
| 5 | + |
| 6 | +const notice = { |
| 7 | + en: { |
| 8 | + title: 'AI-Assisted Content', |
| 9 | + body: 'This article is AI-assisted, and the author has strived for accuracy. Please use with discretion.', |
| 10 | + dateTemplates: { |
| 11 | + today: 'Posted today', |
| 12 | + singular: 'Posted 1 day ago', |
| 13 | + plural: 'Posted {days} days ago' |
| 14 | + } |
| 15 | + }, |
| 16 | + zh: { |
| 17 | + title: 'AI-Assisted Content', |
| 18 | + body: '本文由AI辅助生成,作者已尽力确保内容准确,请谨慎参考', |
| 19 | + dateTemplates: { |
| 20 | + today: '发布于今天', |
| 21 | + singular: '发布于 1 天前', |
| 22 | + plural: '发布于 {days} 天前' |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * A remark plugin to add a notice to posts tagged with 'ai-generation'. |
| 29 | + * |
| 30 | + * @returns {(tree: Node, file: VFile) => void} |
| 31 | + */ |
| 32 | +export function remarkAiNotice() { |
| 33 | + return (tree, file) => { |
| 34 | + const frontmatter = file.data.astro?.frontmatter; |
| 35 | + const aiModels = frontmatter?.['ai-model']; |
| 36 | + const shouldShowNotice = aiModels && ((Array.isArray(aiModels) && aiModels.length > 0) || (typeof aiModels === 'string' && aiModels.trim() !== '')); |
| 37 | + |
| 38 | + if (frontmatter && shouldShowNotice) { |
| 39 | + const lang = frontmatter.language?.toLowerCase() === 'english' ? 'en' : 'zh'; |
| 40 | + const noticeText = notice[lang]; |
| 41 | + |
| 42 | + const modelsArray = Array.isArray(aiModels) ? aiModels : [aiModels]; |
| 43 | + const modelsHtml = ` |
| 44 | + <div class="ai-notice-models"> |
| 45 | + ${modelsArray.map(model => `<span class="ai-model-tag">${model}</span>`).join('')} |
| 46 | + </div>`; |
| 47 | + |
| 48 | + const pubDate = frontmatter.publishDate || frontmatter.pubDate || frontmatter.date |
| 49 | + let dateHtml = '' |
| 50 | + if (pubDate) { |
| 51 | + const date = new Date(pubDate) |
| 52 | + dateHtml = `<span class="publish-date" |
| 53 | + data-publish-date="${date.toISOString()}" |
| 54 | + data-template-today="${noticeText.dateTemplates.today}" |
| 55 | + data-template-singular="${noticeText.dateTemplates.singular}" |
| 56 | + data-template-plural="${noticeText.dateTemplates.plural}"> |
| 57 | + </span>` |
| 58 | + } |
| 59 | + |
| 60 | + const noticeNode = { |
| 61 | + type: 'html', |
| 62 | + value: ` |
| 63 | +<div class="ai-notice not-prose"> |
| 64 | + <div class="ai-notice-robot-icon"> |
| 65 | + <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 8V4H8"/><rect width="16" height="12" x="4" y="8" rx="2"/><path d="M2 14h2"/><path d="M20 14h2"/><path d="M15 13v2"/><path d="M9 13v2"/></svg> |
| 66 | + </div> |
| 67 | + <div class="ai-notice-text"> |
| 68 | + <p><strong>${noticeText.title}</strong></p> |
| 69 | + <p>${noticeText.body}</p> |
| 70 | + </div> |
| 71 | + ${(modelsHtml || dateHtml) ? |
| 72 | + `<div class="ai-notice-footer"> |
| 73 | + ${modelsHtml} |
| 74 | + ${dateHtml} |
| 75 | + </div>` : ''} |
| 76 | +</div> |
| 77 | +<script> |
| 78 | + if (!window.hasInitializedAiNoticeScript) { |
| 79 | + const calculateDays = () => { |
| 80 | + const dateElements = document.querySelectorAll('.publish-date[data-publish-date]'); |
| 81 | + if (dateElements.length === 0) return; |
| 82 | +
|
| 83 | + const today = new Date(); |
| 84 | + today.setHours(0, 0, 0, 0); |
| 85 | +
|
| 86 | + dateElements.forEach(el => { |
| 87 | + const pubDateStr = el.dataset.publishDate; |
| 88 | + if (!pubDateStr) return; |
| 89 | +
|
| 90 | + const pubDate = new Date(pubDateStr); |
| 91 | + pubDate.setHours(0, 0, 0, 0); |
| 92 | +
|
| 93 | + const diffTime = today.getTime() - pubDate.getTime(); |
| 94 | + const diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24)); |
| 95 | +
|
| 96 | + if (diffDays < 0) return; |
| 97 | +
|
| 98 | + let text; |
| 99 | + if (diffDays === 0) { |
| 100 | + text = el.dataset.templateToday; |
| 101 | + } else if (diffDays === 1) { |
| 102 | + text = el.dataset.templateSingular; |
| 103 | + } else { |
| 104 | + text = el.dataset.templatePlural.replace('{days}', diffDays); |
| 105 | + } |
| 106 | + el.textContent = text; |
| 107 | + }); |
| 108 | + }; |
| 109 | +
|
| 110 | + if (document.readyState === 'loading') { |
| 111 | + document.addEventListener('DOMContentLoaded', calculateDays); |
| 112 | + } else { |
| 113 | + calculateDays(); |
| 114 | + } |
| 115 | + window.hasInitializedAiNoticeScript = true; |
| 116 | + } |
| 117 | +</script> |
| 118 | +`, |
| 119 | + } |
| 120 | + |
| 121 | + if (tree.children) { |
| 122 | + tree.children.unshift(noticeNode) |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | +} |
0 commit comments