Skip to content

Commit

Permalink
Fix multiple issues with markdown parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Dec 14, 2024
1 parent ddc64e5 commit b45d5ea
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 37 deletions.
1 change: 1 addition & 0 deletions .config/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"categorizeByGroup": false,
"categoryOrder": ["Reflections", "Types", "Comments", "*"],
"groupOrder": ["Common", "Namespaces", "*"],
"hostedBaseUrl": "https://typedoc.org/example/",
"navigationLinks": {
"Docs": "https://typedoc.org",
"Example": "https://typedoc.org/example/index.html",
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ title: Changelog
### Bug Fixes

- Fix restoration of groups/categories including documents, #2801.
- Fixed missed relative paths within markdown link references in documents.
- Improved handling of incomplete inline code blocks within markdown.

### Thanks!

Expand Down
1 change: 1 addition & 0 deletions example/typedoc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"searchGroupBoosts": {
"Classes": 1.5
},
"hostedBaseUrl": "https://typedoc.org/example/",
"navigationLinks": {
"Docs": "https://typedoc.org",
"API": "https://typedoc.org/api/index.html",
Expand Down
2 changes: 1 addition & 1 deletion site/options/input.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ Expects all entry points to be `.json` files generated with a previous run of Ty
Options to set be set within each package when entryPointStrategy is set to
packages. Unlike most options in TypeDoc, paths within this object are
interpreted relative to the package directory. This option has no effect if
[entryPointStrategy](#entrypointstrategy) is not set to `packages.
[entryPointStrategy](#entrypointstrategy) is not set to `packages`.

## alwaysCreateEntryPointModule

Expand Down
6 changes: 4 additions & 2 deletions site/typedoc.config.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
"notExported": false,
},

"hostedBaseUrl": "https://typedoc.org/",
"redirects": {
"guides/": "documents/Overview.html",
"guides/overview/": "documents/Overview.html",
"guides/installation/": "index.html",
"guides/options/": "documents/Options.html",
Expand All @@ -65,7 +67,7 @@
"guides/themes/": "documents/Themes.html",
"guides/plugins/": "documents/Plugins.html",
"guides/declaration-references/": "documents/Declaration_References.html",
"guides/development": "documents/Development.html",
"guides/development/": "documents/Development.html",
"guides/changelog/": "documents/Changelog.html",

// Tags
Expand Down Expand Up @@ -106,7 +108,7 @@
"tags/readonly/": "documents/Tags._readonly.html",
"tags/remarks/": "documents/Tags._remarks.html",
"tags/returns/": "documents/Tags._returns.html",
"tags/satisfies/": "documents/Tags._satisfies.html",
"tags/satisfies/": "documents/Tags.TypeScript_Tags.html",
"tags/sealed/": "documents/Tags._sealed.html",
"tags/see/": "documents/Tags._see.html",
"tags/template/": "documents/Tags._template.html",
Expand Down
38 changes: 28 additions & 10 deletions src/lib/converter/comments/blockLexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,31 @@ function* lexBlockComment2(

case "`": {
// Markdown's code rules are a royal pain. This could be one of several things.
// 1. Inline code: <1-n ticks><text><same number of ticks>
// 2. Code block: <3 ticks><language, no ticks>\n<text>\n<3 ticks>\n
// 1. Inline code: <1-n ticks><text without multiple consecutive newlines or ticks at start of line><same number of ticks>
// 2. Code block: <newline><3+ ticks><language, no ticks>\n<text>\n<3 ticks>\n
// 3. Unmatched tick(s), not code, but part of some text.
// We don't quite handle #2 correctly yet. PR welcome!
braceStartsType = false;
let tickCount = 1;
let lookahead = pos;

let lookahead = pos - 1;
let atNewline = true;
while (lookahead > 0 && file[lookahead] !== "\n") {
if (/\S/.test(file[lookahead])) {
if (!commentHasStars || file[lookahead] !== "*") {
atNewline = false;
break;
}
}
--lookahead;
}
lookahead = pos;

while (lookahead + 1 < end && file[lookahead + 1] === "`") {
tickCount++;
lookahead++;
}
const isCodeBlock = atNewline && tickCount >= 3;
let lookaheadStart = pos;
const codeText: string[] = [];

Expand All @@ -169,12 +182,17 @@ function* lexBlockComment2(
codeText.push(
file.substring(lookaheadStart, lookahead),
);
yield {
kind: TokenSyntaxKind.Code,
text: codeText.join(""),
pos,
};
pos = lookahead;
const codeTextStr = codeText.join("");
if (isCodeBlock || !/\n\s*\n/.test(codeTextStr)) {
yield {
kind: TokenSyntaxKind.Code,
text: codeTextStr,
pos,
};
pos = lookahead;
} else {
yield makeToken(TokenSyntaxKind.Text, tickCount);
}
break;
} else if (file[lookahead] === "`") {
while (lookahead < end && file[lookahead] === "`") {
Expand Down Expand Up @@ -216,7 +234,7 @@ function* lexBlockComment2(

if (lookahead >= end && pos !== lookahead) {
if (
tickCount === 3 &&
isCodeBlock &&
file.substring(pos, end).includes("\n")
) {
codeText.push(file.substring(lookaheadStart, end));
Expand Down
30 changes: 23 additions & 7 deletions src/lib/converter/comments/lineLexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,23 @@ function* lexLineComments2(
// We don't quite handle #2 correctly yet. PR welcome!
braceStartsType = false;
let tickCount = 1;
let lookahead = pos;

let lookahead = pos - 1;
let atNewline = true;
while (lookahead > 0 && file[lookahead] !== "\n") {
if (/\S/.test(file[lookahead])) {
atNewline = false;
break;
}
--lookahead;
}
lookahead = pos;

while (lookahead + 1 < end && file[lookahead + 1] === "`") {
tickCount++;
lookahead++;
}
const isCodeBlock = atNewline && tickCount >= 3;
let lookaheadStart = pos;
const codeText: string[] = [];

Expand All @@ -103,12 +114,17 @@ function* lexLineComments2(
codeText.push(
file.substring(lookaheadStart, lookahead),
);
yield {
kind: TokenSyntaxKind.Code,
text: codeText.join(""),
pos,
};
pos = lookahead;
const codeTextStr = codeText.join("");
if (isCodeBlock || !/\n\s*\n/.test(codeTextStr)) {
yield {
kind: TokenSyntaxKind.Code,
text: codeTextStr,
pos,
};
pos = lookahead;
} else {
yield makeToken(TokenSyntaxKind.Text, tickCount);
}
break;
} else if (file[lookahead] === "`") {
while (lookahead < end && file[lookahead] === "`") {
Expand Down
45 changes: 28 additions & 17 deletions src/lib/converter/comments/rawLexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,16 @@ function* lexCommentString2(
end--;
}

let lineStart = true;
let expectingTag = false;

for (;;) {
if (pos >= end) {
return;
}

if (lineStart) {
lineStart = false;
}

switch (file[pos]) {
case "\n":
yield makeToken(TokenSyntaxKind.NewLine, 1);
lineStart = true;
expectingTag = false;
break;

Expand All @@ -84,17 +78,28 @@ function* lexCommentString2(

case "`": {
// Markdown's code rules are a royal pain. This could be one of several things.
// 1. Inline code: <1-n ticks><text><same number of ticks>
// 2. Code block: <3 ticks><language, no ticks>\n<text>\n<3 ticks>\n
// 1. Inline code: <1-n ticks><text without multiple consecutive newlines or ticks at start of line><same number of ticks>
// 2. Code block: <newline><3+ ticks><language, no ticks>\n<text>\n<3 ticks>\n
// 3. Unmatched tick(s), not code, but part of some text.
// We don't quite handle #2 correctly yet. PR welcome!
let tickCount = 1;
let lookahead = pos;

let lookahead = pos - 1;
let atNewline = true;
while (lookahead > 0 && file[lookahead] !== "\n") {
if (/\S/.test(file[lookahead])) {
atNewline = false;
break;
}
--lookahead;
}
lookahead = pos;

while (lookahead + 1 < end && file[lookahead + 1] === "`") {
tickCount++;
lookahead++;
}
const isCodeBlock = atNewline && tickCount >= 3;
let lookaheadStart = pos;
const codeText: string[] = [];

Expand All @@ -105,13 +110,19 @@ function* lexCommentString2(
codeText.push(
file.substring(lookaheadStart, lookahead),
);
yield {
kind: TokenSyntaxKind.Code,
text: codeText.join(""),
pos,
};
expectingTag = false;
pos = lookahead;
const codeTextStr = codeText.join("");
if (isCodeBlock || !/\n\s*\n/.test(codeTextStr)) {
yield {
kind: TokenSyntaxKind.Code,
text: codeTextStr,
pos,
};
expectingTag = false;
pos = lookahead;
} else {
yield makeToken(TokenSyntaxKind.Text, tickCount);
expectingTag = false;
}
break;
} else if (file[lookahead] === "`") {
while (lookahead < end && file[lookahead] === "`") {
Expand All @@ -136,7 +147,7 @@ function* lexCommentString2(

if (lookahead >= end && pos !== lookahead) {
if (
tickCount === 3 &&
isCodeBlock &&
file.substring(pos, end).includes("\n")
) {
codeText.push(file.substring(lookaheadStart, end));
Expand Down
1 change: 1 addition & 0 deletions src/lib/converter/comments/textParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export function textContent(
continue;
}

data.atNewLine = token.text[data.pos] === "\n";
++data.pos;
}

Expand Down
Loading

0 comments on commit b45d5ea

Please sign in to comment.