Skip to content

Commit 5e20ece

Browse files
authored
Merge pull request #25 from Mermaid-Chart/fix/fix-CRLF-windows-issue
fix(cli): improve handling of Windows CRLF line endings
2 parents 31d45c3 + 4bb82ae commit 5e20ece

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ jobs:
1010
matrix:
1111
node: ['18.18.x']
1212
pkg: ['sdk', 'cli']
13+
os: [ubuntu-latest, windows-latest, macos-latest]
1314
runs-on:
14-
labels: ubuntu-latest
15+
labels: ${{ matrix.os }}
1516
steps:
1617
- name: Checkout
1718
uses: actions/checkout@v4

packages/cli/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
55

66
## [Unreleased]
77

8+
### Fixed
9+
10+
- Improve handling of Windows CRLF line-endings
11+
812
## [0.1.0] - 2024-04-22
913

1014
Initial release.

packages/cli/src/commander.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,10 @@ describe('link', () => {
390390
expect(file).toMatch(idLineRegex);
391391
// other than the added `id: xxxx` field, everything else should be identical,
392392
// although in practice, we'd expect some formatting changes
393-
expect(file.replace(idLineRegex, '')).toStrictEqual(
394-
await readFile(UNUSUAL_MARKDOWN_FILE, { encoding: 'utf8' }),
393+
//
394+
// We also normalize line endings to LF to avoid issues with CRLF on Windows
395+
expect(file.replace(idLineRegex, '').replaceAll('\r\n', '\n')).toStrictEqual(
396+
(await readFile(UNUSUAL_MARKDOWN_FILE, { encoding: 'utf8' })).replaceAll('\r\n', '\n'),
395397
);
396398
});
397399
});

packages/cli/src/frontmatter.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { extractFrontMatter } from './frontmatter.js';
3+
4+
describe('extractFrontMatter()', () => {
5+
it(String.raw`should handle \r\n (␍␊)`, () => {
6+
const text = '---\r\nid: test\r\n---\r\ninfo\r\n';
7+
const result = extractFrontMatter(text);
8+
expect(result.metadata.id).toBe('test');
9+
});
10+
});

packages/cli/src/frontmatter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import { parseDocument, type Document, YAMLMap, isMap } from 'yaml';
55

6-
const frontMatterRegex = /^-{3}\s*[\n\r](.*?)[\n\r]-{3}\s*[\n\r]+/s;
6+
const frontMatterRegex = /^-{3}\s*[\n\r](.*?[\n\r])-{3}\s*[\n\r]+/s;
77
const urlIDRegex = /(?<baseURL>.*)\/d\/(?<documentID>[\w-]+)/;
88

99
type UrlID = `${string}/d/${string}`;

0 commit comments

Comments
 (0)