-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): normalize line breaks and tags within table columns (#615)
- Loading branch information
Showing
16 changed files
with
337 additions
and
32 deletions.
There are no files selected for viewing
24 changes: 9 additions & 15 deletions
24
packages/typedoc-plugin-markdown/src/libs/utils/format-table-column.spec.ts
This file contains 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 |
---|---|---|
@@ -1,22 +1,16 @@ | ||
import { formatTableColumn } from './format-table-column'; | ||
|
||
describe('formatTableColumn', () => { | ||
it('should format table column correctly', () => { | ||
const input = `This is a string with | ||
a newline, | a pipe, and a code block: | ||
\`\`\`ts | ||
const x = 10; | ||
\`\`\``; | ||
const expectedOutput = | ||
'This is a string with<br />a newline, \\| a pipe, and a code block:<br />`const x = 10;`'; | ||
const result = formatTableColumn(input); | ||
expect(result).toEqual(expectedOutput); | ||
it('should correctly escape pipes', () => { | ||
const input = 'This is a test | with a pipe.'; | ||
const expectedOutput = 'This is a test \\| with a pipe.'; | ||
expect(formatTableColumn(input)).toBe(expectedOutput); | ||
}); | ||
|
||
it('should remove trailing <br /> tags', () => { | ||
const input = 'This is a string with a trailing <br /> tag<br /> '; | ||
const expectedOutput = 'This is a string with a trailing <br /> tag'; | ||
const result = formatTableColumn(input); | ||
expect(result).toEqual(expectedOutput); | ||
it('should correctly convert multi-line markdown to HTML', () => { | ||
const input = `1. First item | ||
2. Second item`; | ||
const expectedOutput = '<ol><li>First item</li><li>Second item</li></ol>'; | ||
expect(formatTableColumn(input)).toBe(expectedOutput); | ||
}); | ||
}); |
18 changes: 11 additions & 7 deletions
18
packages/typedoc-plugin-markdown/src/libs/utils/format-table-column.ts
This file contains 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 |
---|---|---|
@@ -1,9 +1,13 @@ | ||
import { markdownBlocksToHtml } from './markdown-blocks-to-html'; | ||
import { normalizeLineBreaks } from './normalize-line-breaks'; | ||
|
||
export function formatTableColumn(str: string) { | ||
return str | ||
.replace(/\|/g, '\\|') | ||
.replace(/\n(?=(?:[^`]*`[^`]*`)*[^`]*$)/gi, '<br />') | ||
.replace(/\`\`\`ts/g, '`') | ||
.replace(/\`\`\`/g, '`') | ||
.replace(/\n/g, '') | ||
.replace(/(<br \/>\s*)+$/g, ''); | ||
// Normalize line breaks | ||
let md = normalizeLineBreaks(str); | ||
// If comments are on multiple lines convert markdown block tags to HTML and remove new lines. | ||
if (md.split('\n').length > 1) { | ||
md = markdownBlocksToHtml(md); | ||
} | ||
// Finally return with escaped pipes | ||
return md.replace(/\|/g, '\\|'); | ||
} |
This file contains 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
35 changes: 35 additions & 0 deletions
35
packages/typedoc-plugin-markdown/src/libs/utils/markdown-blocks-to-html.spec.ts
This file contains 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,35 @@ | ||
import { markdownBlocksToHtml } from './markdown-blocks-to-html'; | ||
|
||
describe('markdownBlocksToHtml', () => { | ||
it('should correctly convert markdown to HTML', () => { | ||
const input = `This is a test | ||
Double new line | ||
### Heading | ||
<h4>Subheading</h4> | ||
- list item 1 | ||
- list item 2`; | ||
|
||
const expectedOutput = `<p>This is a test</p><p>Double new line</p><h3>Heading</h3><h4>Subheading</h4><ul><li>list item 1</li><li>list item 2</li></ul>`; | ||
|
||
expect(markdownBlocksToHtml(input)).toBe(expectedOutput); | ||
}); | ||
|
||
it('should correctly convert markdown to HTML', () => { | ||
const input = `<p>paragraph</p> | ||
New line | ||
<p>paragraph</p> | ||
<p> | ||
paragraph with new line | ||
</p>`; | ||
|
||
const expectedOutput = `<p>paragraph</p><p>New line</p><p>paragraph</p><p>paragraph with new line </p>`; | ||
|
||
expect(markdownBlocksToHtml(input)).toBe(expectedOutput); | ||
}); | ||
}); |
73 changes: 73 additions & 0 deletions
73
packages/typedoc-plugin-markdown/src/libs/utils/markdown-blocks-to-html.ts
This file contains 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,73 @@ | ||
export function markdownBlocksToHtml(markdownText: string) { | ||
// Remove new lines inside <p> tags | ||
markdownText = markdownText.replace(/<p>([\s\S]*?)<\/p>/gm, (match, p1) => { | ||
const contentWithoutNewLinesOrLeadingSpaces = p1 | ||
.replace(/\r?\n|\r/g, ' ') | ||
.replace(/^\s+/, ''); | ||
return `<p>${contentWithoutNewLinesOrLeadingSpaces}</p>`; | ||
}); | ||
|
||
// Replace headers | ||
markdownText = markdownText.replace( | ||
/^(#{1,6})\s*(.*?)\s*$/gm, | ||
(match, p1, p2) => { | ||
const level = p1.length; | ||
return `<h${level}>${p2}</h${level}>`; | ||
}, | ||
); | ||
|
||
// Replace triple code blocks with code | ||
markdownText = markdownText.replace( | ||
/```.*?\n([\s\S]*?)```/gs, | ||
'<code>$1</code>', | ||
); | ||
|
||
// Replace horizontal rules | ||
markdownText = markdownText.replace(/^[-*_]{3,}\s*$/gm, '<hr />'); | ||
|
||
// Replace unordered lists | ||
markdownText = markdownText.replace(/^(\s*-\s+.+$(\r?\n)?)+/gm, (match) => { | ||
const items = match.trim().split('\n'); | ||
const listItems = items | ||
.map((item) => `<li>${item.trim().substring(2)}</li>`) | ||
.join(''); | ||
return `<ul>${listItems}</ul>`; | ||
}); | ||
|
||
// Replace ordered lists | ||
markdownText = markdownText.replace( | ||
/^(\s*\d+\.\s+.+$(\r?\n)?)+/gm, | ||
(match) => { | ||
const items = match.trim().split('\n'); | ||
const listItems = items | ||
.map( | ||
(item) => `<li>${item.trim().substring(item.indexOf('.') + 2)}</li>`, | ||
) | ||
.join(''); | ||
return `<ol>${listItems}</ol>`; | ||
}, | ||
); | ||
|
||
// Replace paragraphs | ||
markdownText = markdownText.replace( | ||
/^(?!.*<[^>]+>)(.+?)(?:(?:\r\n|\r|\n){2,}|$)(?!.*<[^>]+>)/gm, | ||
'<p>$1</p>', | ||
); | ||
|
||
// Replace ordered lists | ||
markdownText = markdownText.replace( | ||
/^(\s*\d+\.\s+.+$(\r?\n)?)+/gm, | ||
(match) => { | ||
const items = match.trim().split('\n'); | ||
const listItems = items | ||
.map( | ||
(item) => `<li>${item.trim().substring(item.indexOf('.') + 1)}</li>`, | ||
) | ||
.join(''); | ||
return `<ol>${listItems}</ol>`; | ||
}, | ||
); | ||
|
||
// Finally remove all new lines | ||
return markdownText.replace(/\n/g, ''); | ||
} |
24 changes: 24 additions & 0 deletions
24
packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.spec.ts
This file contains 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,24 @@ | ||
import { normalizeLineBreaks } from './normalize-line-breaks'; | ||
|
||
describe('normalizeLineBreaks', () => { | ||
it('should correctly concatenate lines', () => { | ||
const input = `This line should be concatenated with the next one. | ||
The next line. | ||
This is the next line double break. | ||
- list item 1 | ||
- list item 2 | ||
This is another test.`; | ||
|
||
const expectedOutput = `This line should be concatenated with the next one. The next line. | ||
This is the next line double break. | ||
- list item 1 | ||
- list item 2 | ||
This is another test.`; | ||
|
||
expect(normalizeLineBreaks(input)).toBe(expectedOutput); | ||
}); | ||
}); |
38 changes: 38 additions & 0 deletions
38
packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.ts
This file contains 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,38 @@ | ||
export function normalizeLineBreaks(str: string): string { | ||
const codeBlocks: string[] = []; | ||
|
||
const placeholder = '\n___CODEBLOCKPLACEHOLDER___\n'; | ||
str = str.replace(/```[\s\S]*?```/g, (match) => { | ||
codeBlocks.push(match); | ||
return placeholder; | ||
}); | ||
|
||
const lines = str.split('\n'); | ||
let result = ''; | ||
for (let i = 0; i < lines.length; i++) { | ||
if (lines[i].length === 0) { | ||
result = result + lines[i] + '\n'; | ||
} else { | ||
if ( | ||
!lines[i].startsWith('#') && | ||
lines[i + 1] && | ||
/^[a-zA-Z`]/.test(lines[i + 1]) | ||
) { | ||
result = result + lines[i] + ' '; | ||
} else { | ||
if (i < lines.length - 1) { | ||
result = result + lines[i] + '\n'; | ||
} else { | ||
result = result + lines[i]; | ||
} | ||
} | ||
} | ||
} | ||
|
||
result = result.replace( | ||
new RegExp(placeholder, 'g'), | ||
() => `${codeBlocks.shift()}` || '', | ||
); | ||
|
||
return result; | ||
} |
This file contains 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 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 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 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 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 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
Oops, something went wrong.