From 3e7dc6c2a5d57d2b7c09176edc12dec44db3b98c Mon Sep 17 00:00:00 2001 From: tgreyuk Date: Tue, 2 Jul 2024 18:40:20 +0100 Subject: [PATCH] fix(core): check new lines are replaced in windows --- .../src/libs/utils/_index.ts | 1 - .../src/libs/utils/format-table-cell.ts | 2 +- .../libs/utils/normalize-line-breaks.spec.ts | 24 ------------ .../src/libs/utils/normalize-line-breaks.ts | 38 ------------------- .../src/libs/utils/remove-line-breaks.ts | 2 +- .../helpers/get-description-for-reflection.ts | 2 +- 6 files changed, 3 insertions(+), 66 deletions(-) delete mode 100644 packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.spec.ts delete mode 100644 packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.ts diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/_index.ts b/packages/typedoc-plugin-markdown/src/libs/utils/_index.ts index 14cb0396f..f71549a4c 100644 --- a/packages/typedoc-plugin-markdown/src/libs/utils/_index.ts +++ b/packages/typedoc-plugin-markdown/src/libs/utils/_index.ts @@ -6,7 +6,6 @@ export * from './format-table-cell'; export * from './get-file-name-with-extension'; export * from './index'; export * from './is-quoted'; -export * from './normalize-line-breaks'; export * from './remove-first-scoped-directory'; export * from './remove-line-breaks'; export * from './sanitize-comments'; diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/format-table-cell.ts b/packages/typedoc-plugin-markdown/src/libs/utils/format-table-cell.ts index 77cdf92b2..e135f7fe3 100644 --- a/packages/typedoc-plugin-markdown/src/libs/utils/format-table-cell.ts +++ b/packages/typedoc-plugin-markdown/src/libs/utils/format-table-cell.ts @@ -5,7 +5,7 @@ */ export function formatTableCell(str: string) { return str - .replace(/\n/g, ' ') + .replace(/\r?\n/g, ' ') .replace( /```(\w+\s)?([\s\S]*?)```/gs, (match, p1, p2) => `\`${p2.trim()}\``, diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.spec.ts b/packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.spec.ts deleted file mode 100644 index 4122cfad7..000000000 --- a/packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.spec.ts +++ /dev/null @@ -1,24 +0,0 @@ -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); - }); -}); diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.ts b/packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.ts deleted file mode 100644 index cb4a533af..000000000 --- a/packages/typedoc-plugin-markdown/src/libs/utils/normalize-line-breaks.ts +++ /dev/null @@ -1,38 +0,0 @@ -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; -} diff --git a/packages/typedoc-plugin-markdown/src/libs/utils/remove-line-breaks.ts b/packages/typedoc-plugin-markdown/src/libs/utils/remove-line-breaks.ts index d7be912e1..10d9655dd 100644 --- a/packages/typedoc-plugin-markdown/src/libs/utils/remove-line-breaks.ts +++ b/packages/typedoc-plugin-markdown/src/libs/utils/remove-line-breaks.ts @@ -1,3 +1,3 @@ export function removeLineBreaks(str: string) { - return str?.replace(/\n/g, ' ').replace(/ {2,}/g, ' '); + return str?.replace(/\r?\n/g, ' ').replace(/ {2,}/g, ' '); } diff --git a/packages/typedoc-plugin-markdown/src/theme/context/helpers/get-description-for-reflection.ts b/packages/typedoc-plugin-markdown/src/theme/context/helpers/get-description-for-reflection.ts index 158650822..d6a2561e1 100644 --- a/packages/typedoc-plugin-markdown/src/theme/context/helpers/get-description-for-reflection.ts +++ b/packages/typedoc-plugin-markdown/src/theme/context/helpers/get-description-for-reflection.ts @@ -12,7 +12,7 @@ export function getDescriptionForReflection( return this.helpers .getCommentParts(comment.summary) ?.split('\n\n')[0] - .replace(/\n/g, ' '); + .replace(/\r?\n/g, ' '); } return null; }