refactor(settings): remove diary and template features

This commit is contained in:
eezd committed 2026-10-24 20:14:28 +08:00
1 parent a26ebea843
commit 849cd358d0
12 files changed
+19 -173

No files matched your search

-21
View File
@@ -1,21 +0,0 @@
// @vitest-environment happy-dom
import { describe, expect, it } from 'vitest'
import { localDate, renderTemplate } from './template'
describe('Markdown 模板', () => {
it('按本地日期替换变量,资源相对新笔记重定位并保留代码示例', () => {
const now = new Date(2026, 9, 22, 8, 5)
const source = '# {{title}}\n{{date}} {{time}}\n![图](assets/a.png) [文档][ref]\n\n[ref]: ../docs/guide.md#标题\n\n`![](assets/a.png)`\n```\n![](assets/a.png)\n```'
expect(localDate(now)).toBe('2026-10-22')
const result = renderTemplate(source, 'Templates/日记.md', 'Daily/2026-10-22.md', now)
expect(result).toContain('# 2026-10-22\n2026-10-22 08:05')
expect(result).toContain('![图](../Templates/assets/a.png)')
expect(result).toContain('[ref]: ../docs/guide.md#%E6%A0%87%E9%A2%98')
expect(result).toContain('`![](assets/a.png)`\n```\n![](assets/a.png)\n```')
})
it('链接实体和 Markdown 转义按真实文件名重定位', () => {
const result = renderTemplate('[文档](a&b.md) [括号](a\\(1\\).md)', 'Templates/日记.md', 'Daily/今天.md')
expect(result).toContain('[文档](../Templates/a%26b.md)')
expect(result).toContain('[括号](../Templates/a(1).md)')
})
})
-25
View File
@@ -1,25 +0,0 @@
import { markdownParser } from './html'
import { decodeLinkDestination } from './links'
export function localDate(now = new Date()): string {
return `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`
}
export function renderTemplate(content: string, template: string, target: string, now = new Date()): string {
const changes: { from: number; to: number; value: string }[] = []
markdownParser.parse(content).iterate({ enter(node) {
if (node.name !== 'URL' || !['Link', 'Image', 'LinkReference'].includes(node.node.parent?.name ?? '')) return
const raw = decodeLinkDestination(content.slice(node.from, node.to).replace(/^<|>$/g, ''))
if (/^(?:[a-z][a-z\d+.-]*:|\/|#)/i.test(raw)) return
const url = new URL(raw, 'https://rawmark.local/' + template.split('/').map(encodeURIComponent).join('/'))
const parts = decodeURIComponent(url.pathname.slice(1)).split('/')
const base = target.slice(0, Math.max(0, target.lastIndexOf('/'))).split('/').filter(Boolean)
while (base.length && base[0] === parts[0]) { base.shift(); parts.shift() }
const relative = [...base.map(() => '..'), ...parts].map(part => encodeURIComponent(part)).join('/') + url.search + url.hash
changes.push({ from: node.from, to: node.to, value: relative })
} })
for (const change of changes.reverse()) content = content.slice(0, change.from) + change.value + content.slice(change.to)
const variables: Record<string, string> = { date: localDate(now), time: `${String(now.getHours()).padStart(2, '0')}:${String(now.getMinutes()).padStart(2, '0')}`, title: target.slice(target.lastIndexOf('/') + 1).replace(/\.md$/, '') }
return content.replace(/\{\{(date|time|title)\}\}/g, (_, key: string) => variables[key])
}