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

[docs-infra] Support backticks in the codeblocks #37950

Merged
merged 4 commits into from
Jul 18, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/pages/experiments/docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

```bash npm
npm install @mui/material @emotion/react @emotion/styled
# `@emotion/react` and `@emotion/styled` are peer dependencies
```

```bash yarn
yarn add @mui/material @emotion/react @emotion/styled
# `@emotion/react` and `@emotion/styled` are peer dependencies
```

</codeblock>
Expand Down
41 changes: 26 additions & 15 deletions packages/markdown/parseMarkdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ function getDescription(markdown) {
return matches[1].trim().replace(/`/g, '');
}

function getCodeblock(content) {
if (content.startsWith('<codeblock')) {
const storageKey = content.match(/^<codeblock [^>]*storageKey=["|'](\S*)["|'].*>/m)?.[1];
const blocks = [...content.matchAll(/^```(\S*) (\S*)\n(.*?)\n```/gmsu)].map(
([, language, tab, code]) => ({ language, tab, code }),
);

const blocksData = blocks.filter(
(block) => block.tab !== undefined && !emptyRegExp.test(block.code),
);

return {
type: 'codeblock',
data: blocksData,
storageKey,
};
}
return undefined;
}

/**
* @param {string} markdown
*/
Expand Down Expand Up @@ -548,21 +568,11 @@ ${headers.hooks
return null;
}
}
if (content.startsWith('<codeblock')) {
const storageKey = content.match(/^<codeblock [^>]*storageKey=["|'](\S*)["|'].*>/m)?.[1];
const blocks = [...content.matchAll(/^```(\S*) (\S*)\n([^`]*)\n```/gmsu)].map(
([, language, tab, code]) => ({ language, tab, code }),
);

const blocksData = blocks.filter(
(block) => block.tab !== undefined && !emptyRegExp.test(block.code),
);

return {
type: 'codeblock',
data: blocksData,
storageKey,
};

const codeblock = getCodeblock(content);

if (codeblock) {
return codeblock;
}

return render(content);
Expand Down Expand Up @@ -617,6 +627,7 @@ module.exports = {
createRender,
getContents,
getDescription,
getCodeblock,
getHeaders,
getTitle,
prepareMarkdown,
Expand Down
89 changes: 89 additions & 0 deletions packages/markdown/parseMarkdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getTitle,
getHeaders,
prepareMarkdown,
getCodeblock,
} from './parseMarkdown';

describe('parseMarkdown', () => {
Expand Down Expand Up @@ -158,6 +159,45 @@ authors:
]);
});
});

describe('Split markdown into an array, separating codeblocks', () => {
it('uses a `<codeblock>` tag to split', () => {
expect(
getContents(
[
'## Tabs',
'',
'<codeblock storageKey="package-manager">',
'',
'```bash npm',
'npm install @mui/material @emotion/react @emotion/styled',
'```',
'',
'```bash yarn',
'yarn add @mui/material @emotion/react @emotion/styled',
'```',
'',
'</codeblock>',
].join('\n'),
),
).to.deep.equal([
'## Tabs\n\n',
[
'<codeblock storageKey="package-manager">',
'',
'```bash npm',
'npm install @mui/material @emotion/react @emotion/styled',
'```',
'',
'```bash yarn',
'yarn add @mui/material @emotion/react @emotion/styled',
'```',
'',
'</codeblock>',
].join('\n'),
]);
});
});
});

describe('prepareMarkdown', () => {
Expand Down Expand Up @@ -523,6 +563,55 @@ https://ahrefs.com/blog/meta-description/#4-be-concise
});
});

describe('getCodeblock', () => {
it('should return undefined if no codeblock found', () => {
const codeblock = getCodeblock('## Tabs');
expect(codeblock).to.equal(undefined);
});

it('should return the codeblock', () => {
const codeblock = getCodeblock(
[
'<codeblock storageKey="package-manager">',
'',
'```bash npm',
'npm install @mui/material @emotion/react @emotion/styled',
'# `@emotion/react` and `@emotion/styled` are peer dependencies',
'```',
'',
'```sh yarn',
'yarn add @mui/material @emotion/react @emotion/styled',
'# `@emotion/react` and `@emotion/styled` are peer dependencies',
'```',
'',
'</codeblock>',
].join('\n'),
);
expect(codeblock).to.deep.equal({
type: 'codeblock',
storageKey: 'package-manager',
data: [
{
language: 'bash',
tab: 'npm',
code: [
'npm install @mui/material @emotion/react @emotion/styled',
'# `@emotion/react` and `@emotion/styled` are peer dependencies',
].join('\n'),
},
{
language: 'sh',
tab: 'yarn',
code: [
'yarn add @mui/material @emotion/react @emotion/styled',
'# `@emotion/react` and `@emotion/styled` are peer dependencies',
].join('\n'),
},
],
});
});
});

it('should not accept sh', () => {
const markdown = `
# Foo
Expand Down