Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix codeblock detection to catch blocks not on first lines #132821

Merged
merged 1 commit into from Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -87,6 +87,54 @@ suite('typescript.previewer', () => {
'*@param* `parámetroConDiacríticos` — this will not');
});

test('Should render @example blocks as code', () => {
assert.strictEqual(
tagsMarkdownPreview([
{
name: 'example',
text: 'code();'
}
], noopToResource),
'*@example* \n```\ncode();\n```'
);
});

test('Should not render @example blocks as code as if they contain a codeblock', () => {
assert.strictEqual(
tagsMarkdownPreview([
{
name: 'example',
text: 'Not code\n```\ncode();\n```'
}
], noopToResource),
'*@example* \nNot code\n```\ncode();\n```'
);
});

test('Should render @example blocks as code if they contain a <caption>', () => {
assert.strictEqual(
tagsMarkdownPreview([
{
name: 'example',
text: '<caption>Not code</caption>\ncode();'
}
], noopToResource),
'*@example* \nNot code\n```\ncode();\n```'
);
});

test('Should not render @example blocks as code if they contain a <caption> and a codeblock', () => {
assert.strictEqual(
tagsMarkdownPreview([
{
name: 'example',
text: '<caption>Not code</caption>\n```\ncode();\n```'
}
], noopToResource),
'*@example* \nNot code\n```\ncode();\n```'
);
});

test('Should render @linkcode symbol name as code', async () => {
assert.strictEqual(
plainWithLinks([
Expand Down Expand Up @@ -128,4 +176,3 @@ suite('typescript.previewer', () => {
'a [`husky`](file:///path/file.ts#L7%2C5) b');
});
});

Expand Up @@ -39,9 +39,9 @@ function getTagBodyText(
return undefined;
}

// Convert to markdown code block if it is not already one
// Convert to markdown code block if it does not already contain one
function makeCodeblock(text: string): string {
if (text.match(/^\s*[~`]{3}/g)) {
if (text.match(/^\s*[~`]{3}/m)) {
return text;
}
return '```\n' + text + '\n```';
Expand All @@ -53,7 +53,7 @@ function getTagBodyText(
// check for caption tags, fix for #79704
const captionTagMatches = text.match(/<caption>(.*?)<\/caption>\s*(\r\n|\n)/);
if (captionTagMatches && captionTagMatches.index === 0) {
return captionTagMatches[1] + '\n\n' + makeCodeblock(text.substr(captionTagMatches[0].length));
return captionTagMatches[1] + '\n' + makeCodeblock(text.substr(captionTagMatches[0].length));
} else {
return makeCodeblock(text);
}
Expand Down