-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
017d12f
commit 1a548bd
Showing
6 changed files
with
179 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { | ||
testProcessor, | ||
unindentString, | ||
} from '../../test-utils/test-processor'; | ||
|
||
describe('program switcher', () => { | ||
it('should show the program switcher correctly', async () => { | ||
const { html } = await testProcessor(` | ||
::::language-switcher | ||
:::r | ||
I am R | ||
::: | ||
:::python | ||
I am Python | ||
::: | ||
:::: | ||
`); | ||
|
||
const expected = unindentString(` | ||
<div class="language-switcher"> | ||
<ul> | ||
<li data-language="r">R</li> | ||
<li data-language="python">Python</li> | ||
</ul> | ||
<div data-language="r" class="language"> | ||
<p>I am R</p> | ||
</div> | ||
<div data-language="python" class="language"> | ||
<p>I am Python</p> | ||
</div> | ||
</div> | ||
`); | ||
|
||
expect(html).toBe(expected); | ||
}); | ||
|
||
it('should only show cli', async () => { | ||
const md = ` | ||
::::language-switcher | ||
:::r | ||
I am R | ||
::: | ||
:::python | ||
I am Python | ||
::: | ||
:::: | ||
`; | ||
const { html } = await testProcessor(md, { | ||
envLanguage: 'r', | ||
}); | ||
|
||
const expected = unindentString(` | ||
<div class="language-switcher"> | ||
<div data-language="r" class="language show"> | ||
<p>I am R</p> | ||
</div> | ||
</div> | ||
`); | ||
|
||
expect(html).toBe(expected); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { Element, ElementContent } from 'hast'; | ||
import { ContainerDirective } from 'mdast-util-directive'; | ||
import { toHast } from 'mdast-util-to-hast'; | ||
import { Node } from 'unist'; | ||
import { visit } from 'unist-util-visit'; | ||
|
||
import { Context } from '../context'; | ||
|
||
const languages = ['r', 'python']; | ||
const titleCase = ['R', 'Python']; | ||
|
||
export function languageSwitcher(ctx: Context) { | ||
const languageFlag = ctx.options.envLanguage; | ||
if (languageFlag !== undefined && !languages.includes(languageFlag)) { | ||
throw new Error( | ||
`[environment]: envLanguage ${languageFlag} should be one of ${languages}`, | ||
); | ||
} | ||
|
||
return (tree: Node) => { | ||
visit(tree, 'containerDirective', (node: ContainerDirective) => { | ||
if (node.name === 'language-switcher') { | ||
const children = []; | ||
if (languageFlag === undefined) { | ||
children.push(processMenu(node)); | ||
} | ||
children.push(...processChildren(node, languageFlag)); | ||
|
||
node.data = { | ||
hProperties: { | ||
className: 'language-switcher', | ||
}, | ||
hChildren: children, | ||
}; | ||
} | ||
}); | ||
}; | ||
} | ||
|
||
function processMenu(parent: ContainerDirective): ElementContent { | ||
const children = parent.children as ContainerDirective[]; | ||
return { | ||
type: 'element', | ||
tagName: 'ul', | ||
properties: {}, | ||
children: children.map((node) => { | ||
const element: ElementContent = { | ||
type: 'element', | ||
tagName: 'li', | ||
properties: { | ||
'data-language': node.name, | ||
}, | ||
children: [ | ||
{ | ||
type: 'text', | ||
value: titleCase[languages.indexOf(node.name)], | ||
}, | ||
], | ||
}; | ||
return element; | ||
}), | ||
}; | ||
} | ||
|
||
function processChildren( | ||
parent: ContainerDirective, | ||
languageFlag: string | undefined, | ||
): ElementContent[] { | ||
const children = parent.children.map((node) => { | ||
const parent = node as ContainerDirective; | ||
if (languages.includes(parent.name)) { | ||
node.data = { | ||
hProperties: { | ||
'data-language': parent.name, | ||
className: [ | ||
'language', | ||
languageFlag === parent.name ? 'show' : '', | ||
], | ||
}, | ||
}; | ||
} | ||
return node; | ||
}); | ||
|
||
let filtered = children; | ||
if (languageFlag !== undefined) { | ||
filtered = filtered.filter((node) => { | ||
const parent = node as ContainerDirective; | ||
return languageFlag === parent.name; | ||
}); | ||
} | ||
|
||
const parentHast = toHast({ | ||
type: 'root', | ||
children: filtered, | ||
}) as Element; | ||
|
||
return parentHast.children; | ||
} |