Skip to content
Merged
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
9 changes: 7 additions & 2 deletions src/components/common/ui/editor/markdown-content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import { memo, useEffect, useMemo, useRef } from 'react';
import { cn } from '@/components/common/ui/(shadcn)/lib/utils';
import { normalizeMarkdownContent } from '@/utils/markdown-content-normalize';
import { isHtmlContent } from '@/utils/markdown-content-shared';
import { normalizeMarkdownForRichRendering } from '@/utils/markdown-rendering-utils';
import {
normalizeMarkdownForRichRendering,
shouldRenderMarkdownAsMarkdown,
} from '@/utils/markdown-rendering-utils';
import hljs from './hljs-setup';
import {
applyPostSanitizeAttributes,
Expand Down Expand Up @@ -65,7 +68,9 @@ function MarkdownContent({

const renderableContent =
normalizeMarkdownForRichRendering(normalizedContent);
const isOriginalHtml = isHtmlContent(renderableContent);
const isOriginalHtml =
isHtmlContent(renderableContent) &&
!shouldRenderMarkdownAsMarkdown(renderableContent);
const normalizedContentWithEmbeds =
replaceStandaloneYouTubeLinksWithEmbeds(renderableContent);

Expand Down
2 changes: 0 additions & 2 deletions src/components/common/ui/rich-text/markdown-content-core.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import {
applyYouTubeIframeAttributes,
replaceStandaloneYouTubeLinksWithEmbeds,
} from '@/components/common/ui/editor/youtube-utils';
import { isHtmlContent } from '@/lib/rich-text/markdown-utils';
import { normalizeMarkdownForRichRendering } from '@/utils/markdown-rendering-utils';

hljs.registerLanguage('kotlin', kotlin);
Expand Down Expand Up @@ -209,7 +208,6 @@ export default function MarkdownContentCore({
}

const renderableContent = normalizeMarkdownForRichRendering(content);
const isOriginalHtml = isHtmlContent(renderableContent);
const contentWithEmbeds =
replaceStandaloneYouTubeLinksWithEmbeds(renderableContent);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ describe('normalizeAdminCourseMarkdownContent', () => {
expect(normalized).toContain(signedImageUrl);
});

it('keeps markdown parsing when lesson text includes HTML examples in code fences', () => {
const normalized = normalizeAdminCourseMarkdownContent(
[
'#IDE #Cursor #HTML',
'',
'### 5단계. 첫 웹페이지 띄우기',
'',
'```markdown',
'<h1>Hello, Zero-One!</h1>',
'<h2>I am a Vibe Coder</h2>',
'<h3>Future Frontier</h3>',
'```',
'',
`![image.png](${signedImageUrl})`,
].join('\n'),
);

expect(normalized).toContain('<h3>');
expect(normalized).toContain('<pre><code class="language-markdown">');
expect(normalized).toContain('&lt;h1&gt;Hello, Zero-One!&lt;/h1&gt;');
expect(normalized).toContain('<img');
expect(normalized).toContain(signedImageUrl);
});

it('keeps normal editor HTML as HTML while stripping unsafe attributes', () => {
const normalized = normalizeAdminCourseMarkdownContent(
'<p class="external" onclick="alert(1)">본문</p><img src="https://example.com/a.png" style="width:100px">',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { COMMUNITY_MARKDOWN_MAX_IMAGE_FILE_SIZE } from '@/types/community/markdo
import { normalizeMarkdownContent } from '@/utils/markdown-content-normalize';
import { isHtmlContent } from '@/utils/markdown-content-shared';
import {
hasRenderableMarkdownSyntax,
recoverMarkdownTextFromTextOnlyHtml,
renderMarkdownToHtml,
shouldRenderMarkdownAsMarkdown,
} from '@/utils/markdown-rendering-utils';

export const ADMIN_COURSE_MARKDOWN_ALLOWED_IMAGE_EXTENSIONS = [
Expand Down Expand Up @@ -138,10 +138,12 @@ export const normalizeAdminCourseMarkdownContent = (content: unknown) => {
return normalizedContent;
}

if (shouldRenderMarkdownAsMarkdown(normalizedContent)) {
return renderMarkdownToHtml(normalizedContent);
}

if (!isHtmlContent(normalizedContent)) {
return hasRenderableMarkdownSyntax(normalizedContent)
? renderMarkdownToHtml(normalizedContent)
: normalizedContent;
return normalizedContent;
}

const recoveredMarkdown =
Expand Down
32 changes: 32 additions & 0 deletions src/utils/markdown-rendering-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
hasRenderableMarkdownSyntax,
recoverMarkdownTextFromTextOnlyHtml,
renderMarkdownToHtml,
shouldRenderMarkdownAsMarkdown,
} from './markdown-rendering-utils';

const signedImageUrl =
Expand Down Expand Up @@ -65,4 +66,35 @@ describe('markdown-rendering-utils', () => {
),
).toBeUndefined();
});

it('treats HTML examples inside fenced code blocks as markdown content', () => {
const markdown = [
'#IDE #Cursor #HTML',
'',
'### 5단계. 첫 웹페이지 띄우기',
'',
'```markdown',
'<h1>Hello, Zero-One!</h1>',
'<h2>I am a Vibe Coder</h2>',
'<h3>Future Frontier</h3>',
'```',
'',
`![image.png](${signedImageUrl})`,
].join('\n');

expect(shouldRenderMarkdownAsMarkdown(markdown)).toBe(true);

const html = renderMarkdownToHtml(markdown);

expect(html).toContain('<h3>');
expect(html).toContain('<pre><code class="language-markdown">');
expect(html).toContain('&lt;h1&gt;Hello, Zero-One!&lt;/h1&gt;');
expect(html).toContain('<img');
});

it('keeps normal editor HTML classified as HTML even if it has text', () => {
expect(
shouldRenderMarkdownAsMarkdown('<p>본문</p><img src="/images/a.png">'),
).toBe(false);
});
});
23 changes: 23 additions & 0 deletions src/utils/markdown-rendering-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const MARKDOWN_TABLE_PATTERN = /^\s*\|?.+\|.+\r?\n\s*\|?\s*:?-{3,}:?\s*\|/m;

const TEXT_ONLY_HTML_TAGS = new Set(['p', 'br']);
const HTML_TAG_PATTERN = /<\/?([a-z][a-z0-9-]*)\b[^>]*>/gi;
const HTML_LEADING_TAG_PATTERN = /^<[a-z][a-z0-9-]*(?:\s[^<>]*?)?>/i;
const MARKDOWN_CODE_FENCE_BLOCK_PATTERN = /```[\s\S]*?```/g;

export const hasRenderableMarkdownSyntax = (content: unknown) => {
const value = toNonEmptyTrimmedString(content);
Expand All @@ -40,6 +42,27 @@ export const renderMarkdownToHtml = (content: string) => {
return typeof rendered === 'string' ? rendered.trim() : '';
};

export const shouldRenderMarkdownAsMarkdown = (content: unknown) => {
const value = toNonEmptyTrimmedString(content);
if (!value || !hasRenderableMarkdownSyntax(value)) {
return false;
}

if (!isHtmlContent(value)) {
return true;
}

const contentWithoutFencedCode = value.replace(
MARKDOWN_CODE_FENCE_BLOCK_PATTERN,
'',
);
if (!isHtmlContent(contentWithoutFencedCode)) {
return true;
}

return !HTML_LEADING_TAG_PATTERN.test(value);
};

export const recoverMarkdownTextFromTextOnlyHtml = (
content: unknown,
): string | undefined => {
Expand Down
Loading