Skip to content

Commit e6c3d27

Browse files
committed
feat(mdjs-core): support js client as an alias to js script
1 parent f9014c1 commit e6c3d27

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

.changeset/four-pets-brush.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mdjs/core': patch
3+
---
4+
5+
Support `js client` as an alias to `js script`

packages/mdjs-core/src/mdjsParse.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ function mdjsParse() {
1616
if (node.lang === 'js' && node.meta === 'script') {
1717
jsCode += node.value;
1818
}
19+
if (node.lang === 'js' && node.meta === 'client') {
20+
jsCode += node.value;
21+
}
1922
});
2023
// we can only return/modify the tree but jsCode should not be part of the tree
2124
// so we attach it globally to the file.data
@@ -26,7 +29,9 @@ function mdjsParse() {
2629
* @param {Node} node
2730
*/
2831
const removeFunction = node =>
29-
node.type === 'code' && node.lang === 'js' && node.meta === 'script';
32+
node.type === 'code' &&
33+
node.lang === 'js' &&
34+
(node.meta === 'script' || node.meta === 'client');
3035
remove(tree, removeFunction);
3136

3237
return tree;

packages/mdjs-core/test-node/mdjsParse.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ describe('mdjsParse', () => {
2828
expect(/** @type {MDJSVFileData} */ (result.data).jsCode).to.equal('const bar = 22;');
2929
});
3030

31+
it('extracts "js client" code blocks', async () => {
32+
const input = [
33+
'## Intro',
34+
'```js',
35+
'const foo = 1;',
36+
'```',
37+
'```js client',
38+
'const bar = 22;',
39+
'```',
40+
].join('\n');
41+
const parser = unified().use(markdown).use(mdjsParse).use(html, { sanitize: false });
42+
const result = await parser.process(input);
43+
expect(result.contents).to.equal(
44+
'<h2>Intro</h2>\n<pre><code class="language-js">const foo = 1;\n</code></pre>\n',
45+
);
46+
expect(/** @type {MDJSVFileData} */ (result.data).jsCode).to.equal('const bar = 22;');
47+
});
48+
3149
// TODO: fix this bug - maybe something in unified itself 🤔
3250
it.skip('handling only "js script" code blocks', async () => {
3351
const input = [

0 commit comments

Comments
 (0)