generated from executablebooks/markdown-it-plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
✨ Colon fence renderer #39
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,137 @@ | ||
import type MarkdownIt from "markdown-it/lib" | ||
import type Renderer from "markdown-it/lib/renderer" | ||
import type StateBlock from "markdown-it/lib/rules_block/state_block" | ||
import { escapeHtml, unescapeAll } from "markdown-it/lib/common/utils" | ||
|
||
// Ported from: https://github.com/executablebooks/mdit-py-plugins/blob/master/mdit_py_plugins/colon_fence.py | ||
|
||
function _rule(state: StateBlock, startLine: number, endLine: number, silent: boolean) { | ||
let haveEndMarker = false | ||
let pos = state.bMarks[startLine] + state.tShift[startLine] | ||
let maximum = state.eMarks[startLine] | ||
|
||
// if it's indented more than 3 spaces, it should be a code block | ||
if (state.sCount[startLine] - state.blkIndent >= 4) { | ||
return false | ||
} | ||
|
||
if (pos + 3 > maximum) { | ||
return false | ||
} | ||
|
||
const marker = state.src.charCodeAt(pos) | ||
|
||
// /* : */ | ||
if (marker !== 0x3a) { | ||
return false | ||
} | ||
|
||
// scan marker length | ||
let mem = pos | ||
pos = state.skipChars(pos, marker) | ||
|
||
let length = pos - mem | ||
|
||
if (length < 3) { | ||
return false | ||
} | ||
|
||
const markup = state.src.slice(mem, pos) | ||
const params = state.src.slice(pos, maximum) | ||
|
||
// Since start is found, we can report success here in validation mode | ||
if (silent) { | ||
return true | ||
} | ||
|
||
// search end of block | ||
let nextLine = startLine | ||
|
||
// eslint-disable-next-line no-constant-condition | ||
while (true) { | ||
nextLine += 1 | ||
if (nextLine >= endLine) { | ||
// unclosed block should be autoclosed by end of document. | ||
// also block seems to be autoclosed by end of parent | ||
break | ||
} | ||
|
||
pos = mem = state.bMarks[nextLine] + state.tShift[nextLine] | ||
maximum = state.eMarks[nextLine] | ||
|
||
if (pos < maximum && state.sCount[nextLine] < state.blkIndent) { | ||
// non-empty line with negative indent should stop the list: | ||
// - ``` | ||
// test | ||
break | ||
} | ||
|
||
if (state.src.charCodeAt(pos) != marker) { | ||
continue | ||
} | ||
|
||
if (state.sCount[nextLine] - state.blkIndent >= 4) { | ||
// closing fence should be indented less than 4 spaces | ||
continue | ||
} | ||
|
||
pos = state.skipChars(pos, marker) | ||
|
||
// closing code fence must be at least as long as the opening one | ||
if (pos - mem < length) { | ||
continue | ||
} | ||
|
||
// make sure tail has spaces only | ||
pos = state.skipSpaces(pos) | ||
|
||
if (pos < maximum) { | ||
continue | ||
} | ||
|
||
haveEndMarker = true | ||
// found! | ||
break | ||
} | ||
// If a fence has heading spaces, they should be removed from its inner block | ||
length = state.sCount[startLine] | ||
|
||
state.line = nextLine + (haveEndMarker ? 1 : 0) | ||
|
||
const token = state.push("colon_fence", "code", 0) | ||
token.info = params | ||
token.content = state.getLines(startLine + 1, nextLine, length, true) | ||
token.markup = markup | ||
token.map = [startLine, state.line] | ||
|
||
return true | ||
} | ||
|
||
const colonFenceRender: Renderer.RenderRule = function colonFenceRender(tokens, idx) { | ||
const token = tokens[idx] | ||
const info = token.info ? unescapeAll(token.info).trim() : "" | ||
const content = escapeHtml(token.content) | ||
let block_name = "" | ||
|
||
if (info) { | ||
block_name = info.split(" ")[0] | ||
} | ||
const codeClass = block_name ? ` class="block-${block_name}"` : "" | ||
return `<pre><code${codeClass}>${content}</code></pre>\n` | ||
} | ||
|
||
/** | ||
* This plugin directly mimics regular fences, but with `:` colons. | ||
* Example: | ||
* ```md | ||
* :::name | ||
* contained text | ||
* ::: | ||
* ``` | ||
*/ | ||
export function colonFencePlugin(md: MarkdownIt) { | ||
md.block.ruler.before("fence", "colon_fence", _rule, { | ||
alt: ["paragraph", "reference", "blockquote", "list", "footnote_def"] | ||
}) | ||
md.renderer.rules["colon_fence"] = colonFenceRender | ||
} |
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,27 @@ | ||
/* eslint-disable jest/valid-title */ | ||
import MarkdownIt from "markdown-it" | ||
import docutils_plugin from "../src" | ||
import readFixtures from "./readFixtures" | ||
|
||
describe("Parses colonFences", () => { | ||
test("colon fences parse", () => { | ||
const mdit = MarkdownIt().use(docutils_plugin) | ||
const parse = mdit.parse(`:::{tip}\nThis is a tip in a fence!\n:::`, {}) | ||
expect(parse[0].type).toBe("parsed_directive_open") | ||
}) | ||
test("colon fences render", () => { | ||
const mdit = MarkdownIt().use(docutils_plugin) | ||
const rendered = mdit.render(`:::{tip}\nfence\n:::`) | ||
expect(rendered.trim()).toEqual( | ||
'<aside class="admonition tip">\n<header class="admonition-title">Tip</header>\n<p>fence</p>\n</aside>' | ||
) | ||
}) | ||
}) | ||
|
||
describe("Parses fenced directives", () => { | ||
readFixtures("directives.fence").forEach(([name, text, expected]) => { | ||
const mdit = MarkdownIt().use(docutils_plugin) | ||
const rendered = mdit.render(text) | ||
it(name, () => expect(rendered.trim()).toEqual((expected || "").trim())) | ||
}) | ||
}) |
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,36 @@ | ||
Unknown fence directive | ||
. | ||
:::{unknown} argument | ||
content | ||
::: | ||
. | ||
<aside class="directive-unhandled"> | ||
<header><mark>unknown</mark><code> argument</code></header> | ||
<pre>content | ||
</pre></aside> | ||
. | ||
|
||
Fenced admonition: | ||
. | ||
:::{admonition} This is a **title** | ||
An example of an admonition with custom _title_. | ||
::: | ||
. | ||
<aside class="admonition"> | ||
<header class="admonition-title">This is a <strong>title</strong></header> | ||
<p>An example of an admonition with custom <em>title</em>.</p> | ||
</aside> | ||
. | ||
|
||
Fenced note on split lines: | ||
. | ||
:::{note} An example | ||
of an admonition on two lines. | ||
::: | ||
. | ||
<aside class="admonition note"> | ||
<header class="admonition-title">Note</header> | ||
<p>An example | ||
of an admonition on two lines.</p> | ||
</aside> | ||
. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have put the default as
true
here.