feat: 将Markdown渲染改为HTML渲染

- CMS相关字段由Markdown改为WYSIWYG,前端做出对应更改
- AssetUrl重写:将CMS地址重写为本地API
This commit is contained in:
2025-11-14 11:06:00 +08:00
parent 644dfa329c
commit 54d0e297ea
14 changed files with 143 additions and 18 deletions

View File

@ -0,0 +1,14 @@
import { test, expect, describe } from 'vitest';
import { rewriteAssetUrls } from './rewriteAssetUrls';
describe('rewriteAssetUrls', () => {
const cmsBase = 'https://cms.example.com';
const proxyBase = '/api/assets';
const baseHTML =
'<img src="https://cms.example.com/assets/rand-om__-uuid-1234" /><a href="https://cms.example.com/assets/rand-om__-uuid-5678">Link</a><video src="https://otherdomain.com/video.mp4"></video>';
test('rewrites asset URLs correctly', () => {
expect(rewriteAssetUrls(baseHTML, cmsBase, proxyBase)).toBe(
'<img src="/api/assets/rand-om__-uuid-1234"><a href="/api/assets/rand-om__-uuid-5678">Link</a><video src="https://otherdomain.com/video.mp4"></video>'
);
});
});

View File

@ -0,0 +1,36 @@
import { parseDocument, DomUtils } from 'htmlparser2';
import serialize from 'dom-serializer';
export function rewriteAssetUrls(
html: string,
cmsBase: string,
proxyBase: string
) {
if (!html) return html;
const dom = parseDocument(html);
const elements = DomUtils.findAll(
(elem) => elem.type === 'tag',
dom.children
);
for (const el of elements) {
// img / video / source -> src
// a -> href
const tag = el.name.toLowerCase();
const attr = tag === 'a' ? 'href' : 'src';
if (!el.attribs || !el.attribs[attr]) continue;
const url = el.attribs[attr];
// 替换cmsBase为proxyBase
if (url.startsWith(cmsBase)) {
const uuid = url.replace(`${cmsBase}/assets/`, '');
el.attribs[attr] = `${proxyBase}/${uuid}`;
}
}
return serialize(dom);
}