-
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.
added an option
handleMissingLanguageAs
for handling the missing la…
…nguage. Added a test file for it and updated the `README.md` as well.
- Loading branch information
“talatkuyuk”
committed
Jun 16, 2023
1 parent
167940e
commit abea1b6
Showing
5 changed files
with
317 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
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,276 @@ | ||
import { unified, type Processor } from "unified"; | ||
import remarkParse from "remark-parse"; | ||
import gfm from "remark-gfm"; | ||
import remarkRehype from "remark-rehype"; | ||
import rehypeStringify from "rehype-stringify"; | ||
import dedent from "dedent"; | ||
import type { VFileCompatible } from "vfile"; | ||
|
||
import plugin from "../src/index"; | ||
|
||
const compiler: Processor = unified() | ||
.use(remarkParse) | ||
.use(gfm) | ||
.use(plugin, { container: false, handleMissingLanguageAs: "unknown" }) | ||
.use(remarkRehype) | ||
.use(rehypeStringify); | ||
|
||
const process = async (contents: VFileCompatible): Promise<VFileCompatible> => { | ||
return compiler.process(contents).then((file) => file.value); | ||
}; | ||
|
||
describe("remark-flexible-code-title, without container with only title", () => { | ||
// ****************************************** | ||
it("Considers there is no language or title", async () => { | ||
const input = dedent` | ||
\`\`\` | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// { type: 'code', lang: null, meta: null, parent: 'root' } | ||
|
||
const expected = dedent` | ||
<pre><code>const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Considers there is no language or title, but only colon", async () => { | ||
const input = dedent` | ||
\`\`\`: | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// { type: 'code', lang: ':', meta: null, parent: 'root' } ==>> make the lang null | ||
|
||
const expected = dedent` | ||
<pre><code class="language-unknown">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Considers there is only the regular language", async () => { | ||
const input = dedent` | ||
\`\`\`js | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// { type: 'code', lang: 'js', meta: null, parent: 'root' } | ||
|
||
const expected = dedent` | ||
<pre><code class="language-js">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles there is no title but colon", async () => { | ||
const input = dedent` | ||
\`\`\`js: | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// { type: 'code', lang: 'js:', meta: null, parent: 'root' } ==>> delete the colon in the lang | ||
|
||
const expected = dedent` | ||
<pre><code class="language-js">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles there is no language but colon", async () => { | ||
const input = dedent` | ||
\`\`\`:title.js | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// ==>> make the lang null | ||
// { type: 'code', lang: ':title.js', meta: null, parent: 'root' } first visit | ||
// { type: 'code', lang: '', meta: null, parent: 'root' } second visit ==>> see the lang: null | ||
|
||
const expected = dedent` | ||
<div class="remark-code-title">title.js</div> | ||
<pre><code class="language-unknown">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles there is syntax mismatch, having one space around the colon", async () => { | ||
const input = dedent` | ||
\`\`\`js : title.js meta | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
// little complex logic; meta starts with the colon, so remove the first word from meta, if this is not reserved | ||
// { type: 'code', lang: 'js', meta: ': title.js meta', parent: 'root' } ==>> little complex logic; remove title from meta | ||
|
||
const expected = dedent` | ||
<div class="remark-code-title">title.js</div> | ||
<pre><code class="language-js">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles there is syntax mismatch, having more space at the right of the colon", async () => { | ||
const input = dedent` | ||
\`\`\`js: title.js meta | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// ==>> nothing to do, there is no title, but remove the colon from the language at the end | ||
// { type: 'code', lang: 'js:', meta: 'title.js meta', parent: 'root' } | ||
|
||
const expected = dedent` | ||
<div class="remark-code-title">title.js</div> | ||
<pre><code class="language-js">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles there is syntax mismatch, having more space at the both side of the colon", async () => { | ||
const input = dedent` | ||
\`\`\`js : title.js meta | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// little complex logic; meta starts with the colon, so remove the first word from meta, if this is not reserved | ||
// { type: 'code', lang: 'js', meta: ': title.js meta', parent: 'root' } | ||
|
||
const expected = dedent` | ||
<div class="remark-code-title">title.js</div> | ||
<pre><code class="language-js">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles the regular language and the regular title", async () => { | ||
const input = dedent` | ||
\`\`\`js:title.js | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
// { type: 'code', lang: 'js:title.js', meta: null, parent: 'root' } first visit | ||
// { type: 'code', lang: 'js', meta: null, parent: 'root' } second visit | ||
|
||
const expected = dedent` | ||
<div class="remark-code-title">title.js</div> | ||
<pre><code class="language-js">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles unknown language and the line range string within curly braces", async () => { | ||
const input = dedent` | ||
\`\`\`{2, 3 - 4, 5} showLineNumbers | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
/* | ||
{ | ||
type: 'code', | ||
lang: '{2', | ||
meta: ' 3 - 4, 5} showLineNumbers ', | ||
parent: 'root' | ||
} | ||
*/ | ||
|
||
// { title: undefined, language: null, meta: '{2,3-4,5} showLineNumbers' } | ||
|
||
const expected = dedent` | ||
<pre><code class="language-unknown">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles the line range string within curly braces with language", async () => { | ||
const input = dedent` | ||
\`\`\`ts{2 , 3 - 4, 5} showLineNumbers | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
/* | ||
{ | ||
type: 'code', | ||
lang: 'ts{2', | ||
meta: ', 3 - 4, 5} showLineNumbers ', | ||
parent: 'root' | ||
} | ||
*/ | ||
|
||
// { title: undefined, language: 'ts', meta: '{2,3-4,5} showLineNumbers' } | ||
|
||
const expected = dedent` | ||
<pre><code class="language-ts">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).toBe(expected); | ||
}); | ||
|
||
// ****************************************** | ||
it("Handles the line range string within curly braces with language and title", async () => { | ||
const input = dedent` | ||
\`\`\`typescript:title{2 , 3 - 4, 5} showLineNumbers | ||
const a = 1; | ||
\`\`\` | ||
`; | ||
|
||
/* | ||
{ | ||
type: 'code', | ||
lang: 'typescript{2', | ||
meta: ', 3 - 4, 5} showLineNumbers ', | ||
parent: 'root' | ||
} | ||
*/ | ||
|
||
// { title: undefined, language: 'typescript', meta: '{2,3-4,5} showLineNumbers' } | ||
|
||
const expected = dedent` | ||
<div class="remark-code-title">title</div> | ||
<pre><code class="language-typescript">const a = 1; | ||
</code></pre> | ||
`; | ||
|
||
expect(await process(input)).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