-
Notifications
You must be signed in to change notification settings - Fork 0
fix(blog): heading anchor와 글 문체 정리 #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| const MARKDOWN_IMAGE_REGEX = /!\[([^\]]*)\]\([^)]*\)/g; | ||
| const MARKDOWN_LINK_REGEX = /\[([^\]]+)\]\([^)]*\)/g; | ||
| const INLINE_CODE_REGEX = /(`+)([^`]*?)\1/g; | ||
| const HTML_TAG_REGEX = /<\/?[A-Za-z][^>]*>/g; | ||
| const ESCAPED_CHARACTER_REGEX = /\\([\\`*_[\]{}()#+\-.!<>|])/g; | ||
| const TRAILING_HEADING_MARKER_REGEX = /\s+#+\s*$/; | ||
|
|
||
| export function normalizeHeadingText(text: string): string { | ||
| let normalizedText = text.trim(); | ||
|
|
||
| if (!normalizedText) { | ||
| return ''; | ||
| } | ||
|
|
||
| normalizedText = normalizedText | ||
| .replace(MARKDOWN_IMAGE_REGEX, '$1') | ||
| .replace(MARKDOWN_LINK_REGEX, '$1') | ||
| .replace(INLINE_CODE_REGEX, '$2') | ||
| .replace(HTML_TAG_REGEX, '') | ||
| .replace(TRAILING_HEADING_MARKER_REGEX, ''); | ||
|
|
||
| let previousText: string | null = null; | ||
| while (normalizedText !== previousText) { | ||
| previousText = normalizedText; | ||
| normalizedText = normalizedText | ||
| .replace(/(\*\*|__)(.*?)\1/g, '$2') | ||
| .replace(/(\*|_)(.*?)\1/g, '$2') | ||
| .replace(/~~(.*?)~~/g, '$1'); | ||
| } | ||
|
|
||
| return normalizedText | ||
| .replace(ESCAPED_CHARACTER_REGEX, '$1') | ||
| .replace(/\s+/g, ' ') | ||
| .trim(); | ||
| } | ||
|
|
||
| function buildHeadingSlug(text: string): string { | ||
| return normalizeHeadingText(text) | ||
| .toLowerCase() | ||
| .replace( | ||
| /[^\w\uAC00-\uD7AF\u1100-\u11FF\u3130-\u318F\uA960-\uA97F\uD7B0-\uD7FF\s-]/g, | ||
| '' | ||
| ) | ||
| .trim() | ||
| .replace(/\s+/g, '-') | ||
| .replace(/-+/g, '-'); | ||
| } | ||
|
|
||
| export function createHeadingIdGenerator() { | ||
| const idCounts: Record<string, number> = {}; | ||
|
|
||
| return (text: string): string | null => { | ||
| const baseId = buildHeadingSlug(text); | ||
|
|
||
| if (!baseId) { | ||
| return null; | ||
| } | ||
|
|
||
| const count = idCounts[baseId] ?? 0; | ||
| idCounts[baseId] = count + 1; | ||
|
|
||
| return count === 0 ? baseId : `${baseId}-${count}`; | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { render, screen } from '@testing-library/react'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { getMDXComponents } from './components'; | ||
|
|
||
| describe('getMDXComponents', () => { | ||
| it('assigns normalized heading ids from rendered text', () => { | ||
| const { h3: H3, h4: H4 } = getMDXComponents({}); | ||
|
|
||
| if (!H3 || !H4) { | ||
| throw new Error('Expected heading components to be defined'); | ||
| } | ||
|
|
||
| render( | ||
| <> | ||
| <H3> | ||
| <strong> | ||
| 🚀 성능 증명 <code>k6</code> | ||
| </strong> | ||
| </H3> | ||
| <H3>🚀 성능 증명 k6</H3> | ||
| <H4>하위 섹션</H4> | ||
| </> | ||
| ); | ||
|
|
||
| const headings = screen.getAllByRole('heading', { | ||
| name: '🚀 성능 증명 k6', | ||
| }); | ||
|
|
||
| expect(headings).toHaveLength(2); | ||
| expect(headings[0]).toHaveAttribute('id', '성능-증명-k6'); | ||
| expect(headings[1]).toHaveAttribute('id', '성능-증명-k6-1'); | ||
| expect(screen.getByRole('heading', { name: '하위 섹션' })).toHaveAttribute( | ||
| 'id', | ||
| '하위-섹션' | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For longer posts on desktop (I checked existing posts such as
posts/블로그-시스템-구축기/index.mdxwith 41 headings andposts/알고리즘-시각화/index.mdxwith 36), this fixed TOC can extend below the viewport and the lower links become unreachable because the scrollableh-full overflow-y-autocontainer and bottom constraint were removed. Please keep a viewport-bounded height/max-height withoverflow-y-autoso all TOC entries remain accessible.Useful? React with 👍 / 👎.