2024-02-12 07:25:29 -05:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { describe, it } from 'node:test';
|
2023-11-14 10:00:17 -05:00
|
|
|
import { createMarkdownProcessor, createShikiHighlighter } from '../dist/index.js';
|
2023-10-04 05:23:58 -05:00
|
|
|
|
2023-11-08 09:42:05 -05:00
|
|
|
describe('shiki syntax highlighting', () => {
|
2023-10-04 05:23:58 -05:00
|
|
|
it('does not add is:raw to the output', async () => {
|
2023-11-08 09:42:05 -05:00
|
|
|
const processor = await createMarkdownProcessor();
|
2023-10-04 05:26:21 -05:00
|
|
|
const { code } = await processor.render('```\ntest\n```');
|
2023-10-04 05:23:58 -05:00
|
|
|
|
2024-02-12 07:25:29 -05:00
|
|
|
assert.ok(!code.includes('is:raw'));
|
2023-10-04 05:23:58 -05:00
|
|
|
});
|
2023-11-08 09:42:05 -05:00
|
|
|
|
|
|
|
it('supports light/dark themes', async () => {
|
|
|
|
const processor = await createMarkdownProcessor({
|
|
|
|
shikiConfig: {
|
2024-03-08 05:53:19 -05:00
|
|
|
themes: {
|
2023-11-08 09:42:05 -05:00
|
|
|
light: 'github-light',
|
|
|
|
dark: 'github-dark',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { code } = await processor.render('```\ntest\n```');
|
|
|
|
|
|
|
|
// light theme is there:
|
2024-02-12 07:25:29 -05:00
|
|
|
assert.match(code, /background-color:/);
|
|
|
|
assert.match(code, /github-light/);
|
|
|
|
|
2023-11-08 09:42:05 -05:00
|
|
|
// dark theme is there:
|
2024-02-12 07:25:29 -05:00
|
|
|
assert.match(code, /--shiki-dark-bg:/);
|
|
|
|
assert.match(code, /github-dark/);
|
2023-11-08 09:42:05 -05:00
|
|
|
});
|
2023-11-14 10:00:17 -05:00
|
|
|
|
|
|
|
it('createShikiHighlighter works', async () => {
|
|
|
|
const highlighter = await createShikiHighlighter();
|
|
|
|
|
2024-04-01 09:52:50 -05:00
|
|
|
const html = await highlighter.highlight('const foo = "bar";', 'js');
|
2023-11-14 10:00:17 -05:00
|
|
|
|
2024-02-12 07:25:29 -05:00
|
|
|
assert.match(html, /astro-code github-dark/);
|
|
|
|
assert.match(html, /background-color:#24292e;color:#e1e4e8;/);
|
2023-11-14 10:00:17 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
it('diff +/- text has user-select: none', async () => {
|
|
|
|
const highlighter = await createShikiHighlighter();
|
|
|
|
|
2024-04-01 09:52:50 -05:00
|
|
|
const html = await highlighter.highlight(
|
2023-11-14 10:00:17 -05:00
|
|
|
`\
|
|
|
|
- const foo = "bar";
|
|
|
|
+ const foo = "world";`,
|
|
|
|
'diff',
|
|
|
|
);
|
2024-02-12 07:25:29 -05:00
|
|
|
|
|
|
|
assert.match(html, /user-select: none/);
|
|
|
|
assert.match(html, />-<\/span>/);
|
|
|
|
assert.match(html, />+<\/span>/);
|
2023-11-14 10:00:17 -05:00
|
|
|
});
|
2024-03-20 06:15:07 -05:00
|
|
|
|
|
|
|
it('renders attributes', async () => {
|
|
|
|
const highlighter = await createShikiHighlighter();
|
|
|
|
|
2024-04-01 09:52:50 -05:00
|
|
|
const html = await highlighter.highlight(`foo`, 'js', {
|
2024-03-20 06:15:07 -05:00
|
|
|
attributes: { 'data-foo': 'bar', autofocus: true },
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.match(html, /data-foo="bar"/);
|
|
|
|
assert.match(html, /autofocus(?!=)/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('supports transformers that reads meta', async () => {
|
|
|
|
const highlighter = await createShikiHighlighter({
|
|
|
|
transformers: [
|
|
|
|
{
|
|
|
|
pre(node) {
|
|
|
|
const meta = this.options.meta?.__raw;
|
|
|
|
if (meta) {
|
|
|
|
node.properties['data-test'] = meta;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-04-01 09:52:50 -05:00
|
|
|
const html = await highlighter.highlight(`foo`, 'js', {
|
2024-03-20 06:15:07 -05:00
|
|
|
meta: '{1,3-4}',
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.match(html, /data-test="\{1,3-4\}"/);
|
|
|
|
});
|
2024-07-17 09:55:04 -05:00
|
|
|
|
|
|
|
it('supports the defaultColor setting', async () => {
|
|
|
|
const processor = await createMarkdownProcessor({
|
|
|
|
shikiConfig: {
|
|
|
|
themes: {
|
|
|
|
light: 'github-light',
|
|
|
|
dark: 'github-dark',
|
|
|
|
},
|
|
|
|
defaultColor: false,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const { code } = await processor.render('```\ntest\n```');
|
|
|
|
|
|
|
|
// Doesn't have `color` or `background-color` properties.
|
|
|
|
assert.doesNotMatch(code, /color:/);
|
|
|
|
});
|
2024-10-09 04:51:38 -05:00
|
|
|
|
|
|
|
it('the highlighter supports lang alias', async () => {
|
|
|
|
const highlighter = await createShikiHighlighter({
|
|
|
|
langAlias: {
|
|
|
|
cjs: 'javascript',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const html = await highlighter.highlight(`let test = "some string"`, 'cjs', {
|
|
|
|
attributes: { 'data-foo': 'bar', autofocus: true },
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.match(html, /data-language="cjs"/);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('the markdown processsor support lang alias', async () => {
|
|
|
|
const processor = await createMarkdownProcessor({
|
|
|
|
shikiConfig: {
|
|
|
|
langAlias: {
|
|
|
|
cjs: 'javascript',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const { code } = await processor.render('```cjs\nlet foo = "bar"\n```');
|
|
|
|
|
|
|
|
assert.match(code, /data-language="cjs"/);
|
|
|
|
});
|
2023-10-04 05:26:21 -05:00
|
|
|
});
|