-
Notifications
You must be signed in to change notification settings - Fork 0
Add markdown auto-format shortcut #2
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
Closed
mvogttech
wants to merge
1
commit into
master
from
codex/add-auto-format-command-for-markdown-cz3fy8
Closed
Changes from all commits
Commits
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
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,94 @@ | ||
| export function formatMarkdown(content) { | ||
| if (typeof content !== 'string' || content.length === 0) { | ||
| return ''; | ||
| } | ||
|
|
||
| const normalized = content.replace(/\r\n/g, '\n').replace(/\r/g, '\n'); | ||
| const lines = normalized.split('\n'); | ||
| const formatted = []; | ||
|
|
||
| let blankLineCount = 0; | ||
| let inCodeFence = false; | ||
|
|
||
| for (let i = 0; i < lines.length; i++) { | ||
| const originalLine = lines[i]; | ||
| const trimmedEnd = originalLine.replace(/\s+$/u, ''); | ||
| const fenceMatch = trimmedEnd.trimStart().match(/^(?:```+|~~~+)(.*)$/); | ||
|
|
||
| if (inCodeFence) { | ||
| formatted.push(originalLine); | ||
| if (fenceMatch) { | ||
| inCodeFence = false; | ||
| blankLineCount = 0; | ||
| } | ||
| continue; | ||
| } | ||
|
|
||
| if (fenceMatch) { | ||
| formatted.push(trimmedEnd); | ||
| inCodeFence = true; | ||
| blankLineCount = 0; | ||
| continue; | ||
| } | ||
|
|
||
| if (trimmedEnd.trim() === '') { | ||
| if (formatted.length === 0) { | ||
| continue; | ||
| } | ||
| blankLineCount += 1; | ||
| if (blankLineCount > 1) { | ||
| continue; | ||
| } | ||
| formatted.push(''); | ||
| continue; | ||
| } | ||
|
|
||
| blankLineCount = 0; | ||
| let processedLine = trimmedEnd; | ||
|
|
||
| const headingMatch = processedLine.match(/^(#{1,6})(\s*)(.*)$/); | ||
| if (headingMatch) { | ||
| const [, hashes, , text] = headingMatch; | ||
| const headingText = text.trim(); | ||
| processedLine = headingText ? `${hashes} ${headingText}` : hashes; | ||
| formatted.push(processedLine); | ||
| continue; | ||
| } | ||
|
|
||
| const blockquoteMatch = processedLine.match(/^(\s*>+)(\s*)(.*)$/); | ||
| if (blockquoteMatch) { | ||
| const [, markers, , text] = blockquoteMatch; | ||
| const quoteText = text.replace(/^\s+/, ''); | ||
| processedLine = quoteText ? `${markers} ${quoteText}` : markers; | ||
| formatted.push(processedLine); | ||
| continue; | ||
| } | ||
|
|
||
| const listMatch = processedLine.match(/^(\s*)(?:([-*+])|(\d+\.))(\s*)(.*)$/); | ||
| if (listMatch) { | ||
| const indent = listMatch[1] || ''; | ||
| const bullet = listMatch[2] || listMatch[3] || ''; | ||
| const listText = (listMatch[5] || '').replace(/^\s+/, ''); | ||
| const spacer = listText ? ' ' : ''; | ||
| processedLine = `${indent}${bullet}${spacer}${listText}`; | ||
| formatted.push(processedLine); | ||
| continue; | ||
| } | ||
|
|
||
| formatted.push(processedLine); | ||
| } | ||
|
|
||
| while (formatted.length > 0 && formatted[formatted.length - 1] === '') { | ||
| formatted.pop(); | ||
| } | ||
|
|
||
| if (formatted.length === 0) { | ||
| return ''; | ||
| } | ||
|
|
||
| let result = formatted.join('\n'); | ||
| if (!result.endsWith('\n')) { | ||
| result += '\n'; | ||
| } | ||
| return result; | ||
| } | ||
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,21 @@ | ||
| import { formatMarkdown } from '../../src/utils/formatMarkdown.js'; | ||
|
|
||
| describe('formatMarkdown', () => { | ||
| it('normalizes headings and ensures a trailing newline', () => { | ||
| const input = '#Heading\n\nSome text'; | ||
| const output = formatMarkdown(input); | ||
| expect(output).toBe('# Heading\n\nSome text\n'); | ||
| }); | ||
|
|
||
| it('collapses excessive blank lines and trims list spacing', () => { | ||
| const input = '- item one\n\n\n- item two'; | ||
| const output = formatMarkdown(input); | ||
| expect(output).toBe('- item one\n\n- item two\n'); | ||
| }); | ||
|
|
||
| it('preserves code fences without altering inner content', () => { | ||
| const input = '```js\nconst value = 1; \n```\n'; | ||
| const output = formatMarkdown(input); | ||
| expect(output).toBe('```js\nconst value = 1; \n```\n'); | ||
| }); | ||
| }); |
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.
The formatter unconditionally trims all trailing whitespace from every non‑code line (
const trimmedEnd = originalLine.replace(/\s+$/u, '')). In Markdown, two trailing spaces before a newline are the canonical way to force a hard line break. Invoking the new Ctrl+Shift+F shortcut on text that relies on this syntax will remove those spaces and the renderer will collapse the break back into the same paragraph, altering the document’s meaning. The formatter should skip trimming when a line intentionally ends with two spaces.Useful? React with 👍 / 👎.