Skip to content

Commit e2051dc

Browse files
committed
Add a test for CRLF line endings
1 parent 21cf5ae commit e2051dc

File tree

8 files changed

+41
-21
lines changed

8 files changed

+41
-21
lines changed

Diff for: .editorconfig

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ trim_trailing_whitespace = true
1212

1313
[package.json]
1414
indent_size = 2
15+
16+
[src/test/converter2/issues/gh2631/crlf.md]
17+
end_of_line = crlf

Diff for: .gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Disable core.autocrlf's line ending conversion behavior to prevent test failures on Windows
22
* text=auto eol=lf
3+
src/test/converter2/issues/gh2631/crlf.md text=auto eol=crlf
34

45
*.png binary
56
*.psd binary

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
- `@link` tags will now be validated in referenced markdown documents, #2629.
99
- `@link` tags are now resolved in project documents, #2629.
1010
- HTML/JSON output generated by TypeDoc now contains a trailing newline, #2632.
11+
- TypeDoc now correctly handles markdown documents with CRLF line endings, #2628.
1112
- `@hidden` is now properly applied when placed in a function implementation comment, #2634.
1213
- Comments on re-exports are now rendered.
1314

1415
### Thanks!
1516

17+
- @bukowa
1618
- @garrett-hopper
1719

1820
## v0.26.3 (2024-06-28)

Diff for: src/lib/converter/comments/parser.ts

+6-20
Original file line numberDiff line numberDiff line change
@@ -203,42 +203,28 @@ export function parseCommentString(
203203
// Check for frontmatter
204204
let frontmatterData: Record<string, unknown> = {};
205205
const firstBlock = content[0];
206-
207-
let lineBreak: string;
208-
switch (firstBlock.text.startsWith("---\r\n")) {
209-
case true:
210-
lineBreak = "\r\n";
211-
break;
212-
case false:
213-
lineBreak = "\n";
214-
break;
215-
}
216-
217-
if (firstBlock.text.startsWith(`---${lineBreak}`)) {
218-
const end = firstBlock.text.indexOf(`${lineBreak}---${lineBreak}`);
206+
if (firstBlock.text.startsWith("---\n")) {
207+
const end = firstBlock.text.indexOf("\n---\n");
219208
if (end !== -1) {
220-
const yamlText = firstBlock.text.slice(
221-
`---${lineBreak}`.length,
222-
end,
223-
);
209+
const yamlText = firstBlock.text.slice("---\n".length, end);
224210
firstBlock.text = firstBlock.text
225-
.slice(end + `${lineBreak}---${lineBreak}`.length)
211+
.slice(end + "\n---\n".length)
226212
.trimStart();
227213

228214
const frontmatter = parseYamlDoc(yamlText, { prettyErrors: false });
229215
for (const warning of frontmatter.warnings) {
230216
// Can't translate issues coming from external library...
231217
logger.warn(
232218
warning.message as TranslatedString,
233-
warning.pos[0] + `---${lineBreak}`.length,
219+
warning.pos[0] + "---\n".length,
234220
file,
235221
);
236222
}
237223
for (const error of frontmatter.errors) {
238224
// Can't translate issues coming from external library...
239225
logger.error(
240226
error.message as TranslatedString,
241-
error.pos[0] + `---${lineBreak}`.length,
227+
error.pos[0] + "---\n".length,
242228
file,
243229
);
244230
}

Diff for: src/lib/utils/minimalSourceFile.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,20 @@ import { binaryFindPartition } from "./array";
77
const lineStarts = new WeakMap<MinimalSourceFile, number[]>();
88

99
export class MinimalSourceFile implements SourceFileLike {
10+
readonly text: string;
1011
constructor(
11-
readonly text: string,
12+
text: string,
1213
readonly fileName: string,
1314
) {
15+
// This is unfortunate, but the yaml library we use relies on the source
16+
// text using LF line endings https://github.com/eemeli/yaml/issues/127.
17+
// If we don't do this, in a simple document which includes a single key
18+
// like:
19+
// ---<CR><LF>
20+
// title: Windows line endings<CR><LF>
21+
// ---<CR><LF>
22+
// we'll end up with a parsed title of "Windows line endings\r"
23+
this.text = text.replaceAll("\r\n", "\n");
1424
lineStarts.set(this, [0]);
1525
}
1626

Diff for: src/test/converter2/issues/gh2631/crlf.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
title: "Windows Line Endings"
3+
---
4+
5+
This file contains CRLF line endings

Diff for: src/test/converter2/issues/gh2631/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @module
3+
* @document crlf.md
4+
*/
5+
export const a = 123;

Diff for: src/test/issues.c2.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,14 @@ describe("Issue Tests", () => {
16371637
logger.expectNoOtherMessages();
16381638
});
16391639

1640+
it("#2631 handles CRLF line endings in frontmatter", () => {
1641+
const project = convert();
1642+
equal(
1643+
project.documents?.map((d) => d.name),
1644+
["Windows Line Endings"],
1645+
);
1646+
});
1647+
16401648
it("#2634 handles @hidden on function implementations", () => {
16411649
const project = convert();
16421650
equal(project.children?.map((c) => c.name) || [], []);

0 commit comments

Comments
 (0)