2024-02-21 09:08:19 -05:00
|
|
|
import assert from 'node:assert/strict';
|
|
|
|
import { describe, it } from 'node:test';
|
2023-05-24 15:52:22 -05:00
|
|
|
import Markdoc from '@markdoc/markdoc';
|
2024-02-21 09:08:19 -05:00
|
|
|
import { isHTMLString } from 'astro/runtime/server/index.js';
|
|
|
|
import { parseHTML } from 'linkedom';
|
2023-05-25 10:35:07 -05:00
|
|
|
import prism from '../dist/extensions/prism.js';
|
2024-02-21 09:08:19 -05:00
|
|
|
import shiki from '../dist/extensions/shiki.js';
|
2023-05-24 15:52:22 -05:00
|
|
|
import { setupConfig } from '../dist/runtime.js';
|
|
|
|
|
|
|
|
const entry = `
|
|
|
|
\`\`\`ts
|
|
|
|
const highlighting = true;
|
|
|
|
\`\`\`
|
|
|
|
|
|
|
|
\`\`\`css
|
|
|
|
.highlighting {
|
|
|
|
color: red;
|
|
|
|
}
|
|
|
|
\`\`\`
|
|
|
|
`;
|
|
|
|
|
|
|
|
describe('Markdoc - syntax highlighting', () => {
|
2023-05-25 10:35:07 -05:00
|
|
|
describe('shiki', () => {
|
|
|
|
it('transforms with defaults', async () => {
|
|
|
|
const ast = Markdoc.parse(entry);
|
|
|
|
const content = Markdoc.transform(ast, await getConfigExtendingShiki());
|
2023-05-24 15:52:22 -05:00
|
|
|
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(content.children.length, 2);
|
2023-05-25 10:35:07 -05:00
|
|
|
for (const codeBlock of content.children) {
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(isHTMLString(codeBlock), true);
|
2023-05-24 15:52:22 -05:00
|
|
|
|
2023-05-25 10:35:07 -05:00
|
|
|
const pre = parsePreTag(codeBlock);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(pre.classList.contains('astro-code'), true);
|
|
|
|
assert.equal(pre.classList.contains('github-dark'), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('transforms with `theme` property', async () => {
|
|
|
|
const ast = Markdoc.parse(entry);
|
|
|
|
const content = Markdoc.transform(
|
|
|
|
ast,
|
|
|
|
await getConfigExtendingShiki({
|
|
|
|
theme: 'dracula',
|
|
|
|
})
|
|
|
|
);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(content.children.length, 2);
|
2023-05-25 10:35:07 -05:00
|
|
|
for (const codeBlock of content.children) {
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(isHTMLString(codeBlock), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
|
|
|
|
const pre = parsePreTag(codeBlock);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(pre.classList.contains('astro-code'), true);
|
|
|
|
assert.equal(pre.classList.contains('dracula'), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
it('transforms with `wrap` property', async () => {
|
|
|
|
const ast = Markdoc.parse(entry);
|
|
|
|
const content = Markdoc.transform(
|
|
|
|
ast,
|
|
|
|
await getConfigExtendingShiki({
|
|
|
|
wrap: true,
|
|
|
|
})
|
|
|
|
);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(content.children.length, 2);
|
2023-05-25 10:35:07 -05:00
|
|
|
for (const codeBlock of content.children) {
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(isHTMLString(codeBlock), true);
|
2023-05-24 15:52:22 -05:00
|
|
|
|
2023-05-25 10:35:07 -05:00
|
|
|
const pre = parsePreTag(codeBlock);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(pre.getAttribute('style').includes('white-space: pre-wrap'), true);
|
|
|
|
assert.equal(pre.getAttribute('style').includes('word-wrap: break-word'), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
}
|
|
|
|
});
|
2023-05-24 15:52:22 -05:00
|
|
|
});
|
|
|
|
|
2023-05-25 10:35:07 -05:00
|
|
|
describe('prism', () => {
|
|
|
|
it('transforms', async () => {
|
|
|
|
const ast = Markdoc.parse(entry);
|
|
|
|
const config = await setupConfig({
|
|
|
|
extends: [prism()],
|
|
|
|
});
|
|
|
|
const content = Markdoc.transform(ast, config);
|
|
|
|
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(content.children.length, 2);
|
2023-05-25 10:35:07 -05:00
|
|
|
const [tsBlock, cssBlock] = content.children;
|
|
|
|
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(isHTMLString(tsBlock), true);
|
|
|
|
assert.equal(isHTMLString(cssBlock), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
|
|
|
|
const preTs = parsePreTag(tsBlock);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(preTs.classList.contains('language-ts'), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
|
|
|
|
const preCss = parsePreTag(cssBlock);
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.equal(preCss.classList.contains('language-css'), true);
|
2023-05-25 10:35:07 -05:00
|
|
|
});
|
2023-05-24 15:52:22 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {import('astro').ShikiConfig} config
|
|
|
|
* @returns {import('../src/config.js').AstroMarkdocConfig}
|
|
|
|
*/
|
|
|
|
async function getConfigExtendingShiki(config) {
|
2023-05-25 10:35:07 -05:00
|
|
|
return await setupConfig({
|
|
|
|
extends: [shiki(config)],
|
2023-05-24 15:52:22 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} html
|
|
|
|
* @returns {HTMLPreElement}
|
|
|
|
*/
|
|
|
|
function parsePreTag(html) {
|
|
|
|
const { document } = parseHTML(html);
|
|
|
|
const pre = document.querySelector('pre');
|
2024-01-31 05:37:39 -05:00
|
|
|
assert.ok(pre);
|
2023-05-24 15:52:22 -05:00
|
|
|
return pre;
|
|
|
|
}
|