Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions cwd-api/src/api/admin/updateComment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context } from 'hono';
import { Bindings } from '../../bindings';
import { checkContent } from '../public/postComment';
import { marked } from 'marked';
import xss from 'xss';
import { parseMarkdown } from '../../utils/markdown';

export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
let body: any;
Expand Down Expand Up @@ -112,7 +112,7 @@ export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
return c.json({ message: '评论内容不能为空' }, 400);
}

const html = await marked.parse(cleanedContent, { async: true });
const html = await parseMarkdown(cleanedContent);
const contentHtml = xss(html, {
whiteList: {
...xss.whiteList,
Expand All @@ -138,4 +138,3 @@ export const updateComment = async (c: Context<{ Bindings: Bindings }>) => {
message: `Comment updated, id: ${id}.`
});
};

4 changes: 2 additions & 2 deletions cwd-api/src/api/public/postComment.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Context } from 'hono';
import { UAParser } from 'ua-parser-js';
import { marked } from 'marked';
import xss from 'xss';
import { Bindings } from '../../bindings';
import { parseMarkdown } from '../../utils/markdown';
import {
sendCommentNotification,
sendCommentReplyNotification,
Expand Down Expand Up @@ -134,7 +134,7 @@ export const postComment = async (c: Context<{ Bindings: Bindings }>) => {
const name = checkContent(rawName);

// Markdown 渲染与 XSS 过滤
const html = await marked.parse(cleanedContent, { async: true });
const html = await parseMarkdown(cleanedContent);
const contentHtml = xss(html, {
whiteList: {
...xss.whiteList,
Expand Down
8 changes: 8 additions & 0 deletions cwd-api/src/utils/markdown.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { describe, expect, it } from 'vitest';
import { parseMarkdown } from './markdown';

describe('parseMarkdown', () => {
it('renders a single newline as a line break', async () => {
await expect(parseMarkdown('first line\nsecond line')).resolves.toBe('<p>first line<br>second line</p>\n');
});
});
11 changes: 11 additions & 0 deletions cwd-api/src/utils/markdown.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { marked } from 'marked';

/**
* Parse comment Markdown with the same line-break behavior as the widget preview.
*/
export const parseMarkdown = async (content: string): Promise<string> =>
marked.parse(content, {
async: true,
gfm: true,
breaks: true,
});