From 7de7fff4178251a6173ac67b52de50176830f539 Mon Sep 17 00:00:00 2001 From: 0x009922 Date: Fri, 30 Dec 2022 20:29:49 +0700 Subject: [PATCH] fix(build): dedent of a single-line region (#1687) Co-authored-by: Divyansh Singh <40380293+brc-dd@users.noreply.github.com> --- .../node/markdown/plugins/snippet.test.ts | 60 +++++++++++++++++++ package.json | 1 + src/node/markdown/plugins/snippet.ts | 33 ++++------ 3 files changed, 72 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 00000000000..ce50d42bef5 --- /dev/null +++ b/__tests__/unit/node/markdown/plugins/snippet.test.ts @@ -0,0 +1,60 @@ +import { dedent } from 'node/markdown/plugins/snippet' + +describe('node/markdown/plugins/snippet', () => { + describe('dedent', () => { + test('when 0-level is minimal, do not remove spaces', () => { + expect( + dedent( + [ + // + 'fn main() {', + ' println!("Hello");', + '}' + ].join('\n') + ) + ).toMatchInlineSnapshot(` + "fn main() { + println!(\\"Hello\\"); + }" + `) + }) + + test('when 4-level is minimal, remove 4 spaces', () => { + expect( + dedent( + [ + // + ' let a = {', + ' value: 42', + ' };' + ].join('\n') + ) + ).toMatchInlineSnapshot(` + "let a = { + value: 42 + };" + `) + }) + + 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 + };" + `) + }) + }) +}) diff --git a/package.json b/package.json index e8f68ddcccb..37f89bbad33 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 3d2f7cf6692..b80deda5526 100644 --- a/src/node/markdown/plugins/snippet.ts +++ b/src/node/markdown/plugins/snippet.ts @@ -3,29 +3,18 @@ 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 - } +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] !== ' ' && line[i] !== '\t') 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