Skip to content

Commit

Permalink
Merge pull request #25 from Mermaid-Chart/fix/fix-CRLF-windows-issue
Browse files Browse the repository at this point in the history
fix(cli): improve handling of Windows CRLF line endings
  • Loading branch information
aloisklink authored Apr 23, 2024
2 parents 31d45c3 + 4bb82ae commit 5e20ece
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ jobs:
matrix:
node: ['18.18.x']
pkg: ['sdk', 'cli']
os: [ubuntu-latest, windows-latest, macos-latest]
runs-on:
labels: ubuntu-latest
labels: ${{ matrix.os }}
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- Improve handling of Windows CRLF line-endings

## [0.1.0] - 2024-04-22

Initial release.
6 changes: 4 additions & 2 deletions packages/cli/src/commander.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,10 @@ describe('link', () => {
expect(file).toMatch(idLineRegex);
// other than the added `id: xxxx` field, everything else should be identical,
// although in practice, we'd expect some formatting changes
expect(file.replace(idLineRegex, '')).toStrictEqual(
await readFile(UNUSUAL_MARKDOWN_FILE, { encoding: 'utf8' }),
//
// We also normalize line endings to LF to avoid issues with CRLF on Windows
expect(file.replace(idLineRegex, '').replaceAll('\r\n', '\n')).toStrictEqual(
(await readFile(UNUSUAL_MARKDOWN_FILE, { encoding: 'utf8' })).replaceAll('\r\n', '\n'),
);
});
});
Expand Down
10 changes: 10 additions & 0 deletions packages/cli/src/frontmatter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { describe, expect, it } from 'vitest';
import { extractFrontMatter } from './frontmatter.js';

describe('extractFrontMatter()', () => {
it(String.raw`should handle \r\n (␍␊)`, () => {
const text = '---\r\nid: test\r\n---\r\ninfo\r\n';
const result = extractFrontMatter(text);
expect(result.metadata.id).toBe('test');
});
});
2 changes: 1 addition & 1 deletion packages/cli/src/frontmatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { parseDocument, type Document, YAMLMap, isMap } from 'yaml';

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

type UrlID = `${string}/d/${string}`;
Expand Down

0 comments on commit 5e20ece

Please sign in to comment.