Skip to content

Commit

Permalink
[docs-infra] Add test case when using sh
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviertassinari committed Jul 4, 2023
1 parent d0f0606 commit bbd35ce
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
10 changes: 6 additions & 4 deletions packages/markdown/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function getHeaders(markdown) {
return headers;
} catch (err) {
throw new Error(
`docs-infra: ${err.message} in getHeader(markdown) with markdown: \n\n${header}`,
`docs-infra: ${err.message} in getHeader(markdown) with markdown: \n\n${header}\n`,
);
}
}
Expand Down Expand Up @@ -274,7 +274,7 @@ function createRender(context) {
});
} else if (level === 3) {
if (!toc[toc.length - 1]) {
throw new Error(`docs-infra: Missing parent level for: ${headingText}`);
throw new Error(`docs-infra: Missing parent level for: ${headingText}\n`);
}

toc[toc.length - 1].children.push({
Expand Down Expand Up @@ -454,7 +454,7 @@ function prepareMarkdown(config) {
const description = headers.description || getDescription(markdown);

if (title == null || title === '') {
throw new Error(`docs-infra: Missing title in the page: ${location}`);
throw new Error(`docs-infra: Missing title in the page: ${location}\n`);
}

if (title.length > 70) {
Expand All @@ -463,12 +463,13 @@ function prepareMarkdown(config) {
`docs-infra: The title "${title}" is too long (${title.length} characters).`,
'It needs to have fewer than 70 characters—ideally less than 60. For more details, see:',
'https://developers.google.com/search/docs/advanced/appearance/title-link',
'',
].join('\n'),
);
}

if (description == null || description === '') {
throw new Error(`docs-infra: Missing description in the page: ${location}`);
throw new Error(`docs-infra: Missing description in the page: ${location}\n`);
}

if (description.length > 170) {
Expand All @@ -477,6 +478,7 @@ function prepareMarkdown(config) {
`docs-infra: The description "${description}" is too long (${description.length} characters).`,
'It needs to have fewer than 170 characters—ideally less than 160. For more details, see:',
'https://ahrefs.com/blog/meta-description/#4-be-concise',
'',
].join('\n'),
);
}
Expand Down
34 changes: 28 additions & 6 deletions packages/markdown/parseMarkdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,7 @@ authors:
}).to.throw(`docs-infra: Missing trailing slash. The following link:
[foo](/foo) in /test/bar/index.md is missing a trailing slash, please add it.
See https://ahrefs.com/blog/trailing-slash/ for more details.
Please report this to https://github.com/markedjs/marked.`);
See https://ahrefs.com/blog/trailing-slash/ for more details.`);
});

it('should report missing leading splashes', () => {
Expand All @@ -473,9 +471,7 @@ Please report this to https://github.com/markedjs/marked.`);
translations: [{ filename: 'index.md', markdown, userLanguage: 'en' }],
});
}).to.throw(`docs-infra: Missing leading slash. The following link:
[foo](foo/) in /test/bar/index.md is missing a leading slash, please add it.
Please report this to https://github.com/markedjs/marked.`);
[foo](foo/) in /test/bar/index.md is missing a leading slash, please add it.`);
});

it('should report title too long', () => {
Expand Down Expand Up @@ -516,4 +512,30 @@ It needs to have fewer than 170 characters—ideally less than 160. For more det
https://ahrefs.com/blog/meta-description/#4-be-concise`);
});
});

it('should not accept sh', () => {
const markdown = `
# Foo
<p class="description">Fo</p>
\`\`\`sh
npm install @mui/material
\`\`\`
`;

expect(() => {
prepareMarkdown({
...defaultParams,
translations: [{ filename: 'index.md', markdown, userLanguage: 'en' }],
});
}).to.throw(`docs-infra: Unsupported language: "sh" in:
\`\`\`sh
npm install @mui/material
\`\`\`
Use "bash" instead.`);
});
});
13 changes: 12 additions & 1 deletion packages/markdown/prism.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,18 @@ function highlight(code, language) {
break;

case 'sh':
throw new Error([`Unsupported language: "sh", "${code}".`, 'Use "bash" instead.'].join('\n'));
throw new Error(
[
`docs-infra: Unsupported language: "sh" in:`,
'',
'```sh',
code,
'```',
'',
'Use "bash" instead.',
'',
].join('\n'),
);

case 'diff':
prismLanguage = { ...prism.languages.diff };
Expand Down

0 comments on commit bbd35ce

Please sign in to comment.