-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: scenarios for rehype-starry-night-inline plugin
- Loading branch information
1 parent
f9cd12e
commit a8f6321
Showing
6 changed files
with
69 additions
and
0 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
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); | ||
}); |
1 change: 1 addition & 0 deletions
1
test/rehype-starry-night-inline/snapshots/code_element_with_custom_class_name_prefix.html
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 @@ | ||
<p>To prune a remote called origin, run <code class="highlight-inline highlight-sh">git remote prune origin</code></p> |
1 change: 1 addition & 0 deletions
1
test/rehype-starry-night-inline/snapshots/code_element_without_annotation.html
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 @@ | ||
<p><code>System.out.println("Hello, world!")</code></p> |
1 change: 1 addition & 0 deletions
1
test/rehype-starry-night-inline/snapshots/multiple_annotated_code_elements.html
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 @@ | ||
<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> |
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 @@ | ||
<p>Divided we fall.</p> |
1 change: 1 addition & 0 deletions
1
test/rehype-starry-night-inline/snapshots/single_annotated_code_element.html
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 @@ | ||
<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> |