Skip to content

Commit 07a0469

Browse files
authored
Merge pull request microsoft#393 from Microsoft/pgonzal/escape-bug
Fix bug where api-documenter was incorrectly escaping certain special characters
2 parents 9ed9bbd + d1a74b1 commit 07a0469

8 files changed

Lines changed: 42 additions & 14 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ app
1717
app-min
1818
bower_components
1919
coverage
20+
local
2021
dist
2122
jspm_packages
2223
lib
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"packageName": "@microsoft/node-core-library",
5+
"comment": "When FileDiffTest creates a copy of the expected output for comparison, it is now marked as read-only to avoid confusion",
6+
"type": "patch"
7+
}
8+
],
9+
"packageName": "@microsoft/node-core-library",
10+
"email": "pgonzal@users.noreply.github.com"
11+
}

‎libraries/api-documenter/.gitignore‎

Lines changed: 0 additions & 7 deletions
This file was deleted.

‎libraries/api-documenter/src/MarkdownRenderer.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,13 @@ export class MarkdownRenderer {
115115

116116
private static _getEscapedText(text: string): string {
117117
const textWithBackslashes: string = text
118-
.replace('\\', '\\\\') // first replace the escape character
119-
.replace(/[\*\#\[\]\_\|]/g, (x) => '\\' + x); // then escape any special characters
120-
return textWithBackslashes
121-
.replace('&', '&')
122-
.replace('<', '&lt;')
123-
.replace('>', '&gt;');
118+
.replace(/\\/g, '\\\\') // first replace the escape character
119+
.replace(/[*#[\]_|`~]/g, (x) => '\\' + x) // then escape any special characters
120+
.replace(/---/g, '\\-\\-\\-') // hyphens only if it's 3 or more
121+
.replace(/&/g, '&amp;')
122+
.replace(/</g, '&lt;')
123+
.replace(/>/g, '&gt;');
124+
return textWithBackslashes;
124125
}
125126

126127
/**

‎libraries/api-documenter/src/index.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ console.log(os.EOL + colors.bold(`api-documenter ${myPackageJson.version}` + os.
1818

1919
const docItemSet: DocItemSet = new DocItemSet();
2020

21-
const dataFolder: string = path.join(__dirname, '../files');
21+
const dataFolder: string = path.join(__dirname, '../local');
2222

2323
const inputFolder: string = path.join(dataFolder, 'input');
2424
for (const filename of fsx.readdirSync(inputFolder)) {

‎libraries/api-documenter/src/test/ExpectedOutput.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,14 @@ This is a **bold** word.
3030
## Bad characters
3131

3232
**\*one\*two\*three\*four**
33+
34+
## Characters that should be escaped
35+
36+
Double-encoded JSON: "{ \\"A\\": 123}"
37+
38+
HTML chars: &lt;script&gt;alert("\[You\] are \#1!");&lt;/script&gt;
39+
40+
HTML escape: &amp;quot;
41+
42+
3 or more hyphens: - -- \-\-\- \-\-\-- \-\-\--- \-\-\-\-\-\-
43+

‎libraries/api-documenter/src/test/MarkdownPageRenderer.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ describe('MarkdownPageRenderer', () => {
5353
markupPage.elements.push(...MarkupBuilder.createTextElements('*one*two*', { bold: true }));
5454
markupPage.elements.push(...MarkupBuilder.createTextElements('three*four', { bold: true }));
5555

56+
markupPage.elements.push(MarkupBuilder.createHeading1('Characters that should be escaped'));
57+
markupPage.elements.push(...MarkupBuilder.createTextElements(
58+
'Double-encoded JSON: "{ \\"A\\": 123}"\n\n'));
59+
markupPage.elements.push(...MarkupBuilder.createTextElements(
60+
'HTML chars: <script>alert("[You] are #1!");</script>\n\n'));
61+
markupPage.elements.push(...MarkupBuilder.createTextElements(
62+
'HTML escape: &quot;\n\n'));
63+
markupPage.elements.push(...MarkupBuilder.createTextElements(
64+
'3 or more hyphens: - -- --- ---- ----- ------\n\n'));
65+
5666
const outputFilename: string = path.join(outputFolder, 'ActualOutput.md');
5767
fsx.writeFileSync(outputFilename, MarkdownRenderer.renderElements([markupPage], { }));
5868

‎libraries/node-core-library/src/FileDiffTest.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export class FileDiffTest {
7676
+ ' the file already exists:\n' + expectedCopyFilename);
7777
}
7878
fsx.copySync(expectedFilePath, expectedCopyFilename);
79+
fsx.chmodSync(expectedCopyFilename, 292); // 292 = 444 octal = "rrr"
7980

8081
throw new Error('The test output file does not match the expected input:\n'
8182
+ actualFilePath);

0 commit comments

Comments
 (0)