Skip to content

Commit

Permalink
test: scenarios for rehype-starry-night-inline plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
naiyerasif committed Apr 26, 2024
1 parent f9cd12e commit a8f6321
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 0 deletions.
64 changes: 64 additions & 0 deletions test/rehype-starry-night-inline/plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import path from "path";
import { unified } from "unified";
import remarkParse from "remark-parse";
import remarkRehype from "remark-rehype";
import rehypeStringify from "rehype-stringify";
import { expect, it } from "vitest";
import remarkInlineCodeLang from "../../src/remark-inline-code-lang/index.js";
import rehypeStarryNightInline from "../../src/rehype-starry-night-inline/index.js";

const scenarios = [
{
title: "no code element",
input: "Divided we fall."
},
{
title: "code element without annotation",
input: "`System.out.println(\"Hello, world!\")`"
},
{
title: "single annotated code element",
input: "Here's an example of printing a warning: `js> console.warn('WARNING: cease or desist')`"
},
{
title: "multiple annotated code elements",
input: "Launch the container with `sh> docker compose up -d` and run `sh> curl https://localhost:8080`"
},
{
title: "code element with custom class name prefix",
input: "To prune a remote called origin, run `sh> git remote prune origin`",
options: {
classNamePrefix: "highlight"
}
}
];
const scenario = scenarios.map(s => s.title);

async function parse(markdown, options = {}) {
const file = await unified()
.use(remarkParse)
.use(remarkInlineCodeLang)
.use(remarkRehype, { allowDangerousHtml: true })
.use(rehypeStarryNightInline, options)
.use(rehypeStringify, { allowDangerousHtml: true })
.process(markdown);
return String(file);
}

const currentDirectory = process.cwd();
const testDirectory = "test";
const pluginDirectory = "rehype-starry-night-inline";
const snapshotsDirectory = "snapshots";

it.each(scenario)(`Test: %s`, async (rule) => {
const { input, options = {} } = scenarios.find(s => s.title === rule);
const result = await parse(input, options);
const snapshot = path.resolve(
currentDirectory,
testDirectory,
pluginDirectory,
snapshotsDirectory,
`${rule.replaceAll(" ", "_")}.html`
);
await expect(result).toMatchFileSnapshot(snapshot);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>To prune a remote called origin, run <code class="highlight-inline highlight-sh">git remote prune origin</code></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><code>System.out.println("Hello, world!")</code></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Launch the container with <code class="hl-inline hl-sh">docker compose up -d</code> and run <code class="hl-inline hl-sh">curl https://localhost:8080</code></p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Divided we fall.</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Here's an example of printing a warning: <code class="hl-inline hl-js"><span class="pl-en">console</span>.<span class="pl-c1">warn</span>(<span class="pl-s"><span class="pl-pds">'</span>WARNING: cease or desist<span class="pl-pds">'</span></span>)</code></p>

0 comments on commit a8f6321

Please sign in to comment.