From 0c33c54d3af4265dcf8097f6854de5ce0633b761 Mon Sep 17 00:00:00 2001 From: Dmitry Balashov Date: Mon, 12 Dec 2022 09:54:03 +0800 Subject: [PATCH 1/4] fix(snippet): dedent of a single-line region, close #1686 --- .../node/markdown/plugins/snippet.test.ts | 43 +++++++++++++++++++ src/node/markdown/plugins/snippet.ts | 36 ++++++---------- 2 files changed, 57 insertions(+), 22 deletions(-) create mode 100644 __tests__/unit/node/markdown/plugins/snippet.test.ts diff --git a/__tests__/unit/node/markdown/plugins/snippet.test.ts b/__tests__/unit/node/markdown/plugins/snippet.test.ts new file mode 100644 index 000000000000..93adf4b5d4b5 --- /dev/null +++ b/__tests__/unit/node/markdown/plugins/snippet.test.ts @@ -0,0 +1,43 @@ +import { dedent } from 'node/markdown/plugins/snippet' + +describe('node/markdown/plugins/snippet', () => { + describe('dedent', () => { + test("when 0-level is minimal, it doesn't remove spaces", () => { + expect( + dedent( + [ + // + 'fn main() {', + ' println!("Hello");', + '}' + ].join('\n') + ) + ).toMatchInlineSnapshot(` + "fn main() { + println!(\\"Hello\\"); + }" + `) + }) + + test('when 4-level is minimal, it removes 4 spaces', () => { + expect( + dedent( + [ + // + ' let a = {', + ' Some(42)', + ' };' + ].join('\n') + ) + ).toMatchInlineSnapshot(` + "let a = { + Some(42) + };" + `) + }) + + test('when the only 1 line is passed, it is de-dented', () => { + expect(dedent(' let a = 42;')).toEqual('let a = 42;') + }) + }) +}) diff --git a/src/node/markdown/plugins/snippet.ts b/src/node/markdown/plugins/snippet.ts index 3d2f7cf66924..a759598a77f2 100644 --- a/src/node/markdown/plugins/snippet.ts +++ b/src/node/markdown/plugins/snippet.ts @@ -3,29 +3,21 @@ import path from 'path' import MarkdownIt from 'markdown-it' import { RuleBlock } from 'markdown-it/lib/parser_block' -function dedent(text: string) { - const wRegexp = /^([ \t]*)(.*)\n/gm - let match - let minIndentLength = null - - while ((match = wRegexp.exec(text)) !== null) { - const [indentation, content] = match.slice(1) - if (!content) continue - - const indentLength = indentation.length - if (indentLength > 0) { - minIndentLength = - minIndentLength !== null - ? Math.min(minIndentLength, indentLength) - : indentLength - } else break - } +/** + * Exported for tests + */ +export function dedent(text: string): string { + const lines = text.split('\n') + + const minIndentLength = lines.reduce((acc, line) => { + for (let i = 0; i < line.length; i++) { + if (line[i] !== ' ') return Math.min(i, acc) + } + return acc + }, Infinity) - if (minIndentLength) { - text = text.replace( - new RegExp(`^[ \t]{${minIndentLength}}(.*)`, 'gm'), - '$1' - ) + if (minIndentLength < Infinity) { + return lines.map((x) => x.slice(minIndentLength)).join('\n') } return text From e42c2f58fcc8d228be5046cd942e936797223e4c Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 30 Dec 2022 18:44:11 +0530 Subject: [PATCH 2/4] . --- package.json | 1 + src/node/markdown/plugins/snippet.ts | 3 --- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/package.json b/package.json index e8f68ddcccbb..37f89bbad336 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "test-build": "VITE_TEST_BUILD=1 pnpm test-preview", "debug-preview": "DEBUG=1 vitest -r __tests__/e2e", "debug-build": "VITE_TEST_BUILD=1 pnpm debug-preview", + "unit-dev": "vitest -r __tests__/unit", "e2e-dev": "wait-on -d 100 dist/node/cli.js && node ./bin/vitepress dev __tests__/e2e", "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s", "release": "node scripts/release.js", diff --git a/src/node/markdown/plugins/snippet.ts b/src/node/markdown/plugins/snippet.ts index a759598a77f2..ba839632e879 100644 --- a/src/node/markdown/plugins/snippet.ts +++ b/src/node/markdown/plugins/snippet.ts @@ -3,9 +3,6 @@ import path from 'path' import MarkdownIt from 'markdown-it' import { RuleBlock } from 'markdown-it/lib/parser_block' -/** - * Exported for tests - */ export function dedent(text: string): string { const lines = text.split('\n') From 66e800bbe279a8743df4b9ff0451f3c75d86a728 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 30 Dec 2022 18:56:51 +0530 Subject: [PATCH 3/4] handle tabs in dedent --- src/node/markdown/plugins/snippet.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/node/markdown/plugins/snippet.ts b/src/node/markdown/plugins/snippet.ts index ba839632e879..b80deda5526f 100644 --- a/src/node/markdown/plugins/snippet.ts +++ b/src/node/markdown/plugins/snippet.ts @@ -8,7 +8,7 @@ export function dedent(text: string): string { const minIndentLength = lines.reduce((acc, line) => { for (let i = 0; i < line.length; i++) { - if (line[i] !== ' ') return Math.min(i, acc) + if (line[i] !== ' ' && line[i] !== '\t') return Math.min(i, acc) } return acc }, Infinity) From a118605a5fe061c7396f6b4b577d347f09cc7477 Mon Sep 17 00:00:00 2001 From: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> Date: Fri, 30 Dec 2022 18:57:04 +0530 Subject: [PATCH 4/4] add tests --- .../node/markdown/plugins/snippet.test.ts | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/__tests__/unit/node/markdown/plugins/snippet.test.ts b/__tests__/unit/node/markdown/plugins/snippet.test.ts index 93adf4b5d4b5..ce50d42bef55 100644 --- a/__tests__/unit/node/markdown/plugins/snippet.test.ts +++ b/__tests__/unit/node/markdown/plugins/snippet.test.ts @@ -2,7 +2,7 @@ import { dedent } from 'node/markdown/plugins/snippet' describe('node/markdown/plugins/snippet', () => { describe('dedent', () => { - test("when 0-level is minimal, it doesn't remove spaces", () => { + test('when 0-level is minimal, do not remove spaces', () => { expect( dedent( [ @@ -19,25 +19,42 @@ describe('node/markdown/plugins/snippet', () => { `) }) - test('when 4-level is minimal, it removes 4 spaces', () => { + test('when 4-level is minimal, remove 4 spaces', () => { expect( dedent( [ // ' let a = {', - ' Some(42)', + ' value: 42', ' };' ].join('\n') ) ).toMatchInlineSnapshot(` "let a = { - Some(42) + value: 42 };" `) }) - test('when the only 1 line is passed, it is de-dented', () => { + test('when only 1 line is passed, dedent it', () => { expect(dedent(' let a = 42;')).toEqual('let a = 42;') }) + + test('handle tabs as well', () => { + expect( + dedent( + [ + // + ' let a = {', + ' value: 42', + ' };' + ].join('\n') + ) + ).toMatchInlineSnapshot(` + "let a = { + value: 42 + };" + `) + }) }) })