-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b35043b
commit c2cd108
Showing
8 changed files
with
220 additions
and
10 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 |
---|---|---|
|
@@ -12,6 +12,7 @@ module.exports = api => { | |
} | ||
], | ||
'@babel/preset-typescript' | ||
] | ||
], | ||
plugins: ['add-module-exports'] | ||
}; | ||
}; |
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 |
---|---|---|
@@ -1,3 +1,24 @@ | ||
export const callout = (md: any) => { | ||
return null; | ||
import Remarkable from 'remarkable'; | ||
import { TOKENS } from './tokens'; | ||
import { parser } from './parser'; | ||
import { calloutOpenRenderer, calloutCloseRenderer } from './renderer'; | ||
|
||
/** | ||
* Remarkable plugin that recognizes callout syntax in markdown and renders | ||
* it in a dedicated paragraph. | ||
* | ||
* Example syntax: | ||
* | ||
* :::info | ||
* This is an information callout | ||
* ::: | ||
* | ||
* @todo Add opts to customize rendering. | ||
*/ | ||
const plugin: Remarkable.Plugin = (md, opts) => { | ||
md.block.ruler.before('code', TOKENS.CALLOUT, parser, opts); | ||
md.renderer.rules[TOKENS.CALLOUT_OPEN] = calloutOpenRenderer; | ||
md.renderer.rules[TOKENS.CALLOUT_CLOSE] = calloutCloseRenderer; | ||
}; | ||
|
||
export default plugin; |
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 |
---|---|---|
@@ -1,22 +1,110 @@ | ||
import Remarkable from 'remarkable'; | ||
import { parser } from './parser'; | ||
import plugin from '.'; | ||
import dedent from 'dedent'; | ||
|
||
let md!: Remarkable; | ||
|
||
describe('remarkable-callout', () => { | ||
describe('parser', () => { | ||
beforeEach(() => { | ||
md = new Remarkable(); | ||
}); | ||
|
||
test('should render a callout', () => { | ||
md.use(parser); | ||
md.use(plugin); | ||
expect( | ||
md.render(dedent` | ||
:::info | ||
Info | ||
::: | ||
`) | ||
).toEqual(`<p class="callout-info">Info</p>`); | ||
).toMatchInlineSnapshot(` | ||
"<p class=\\"callout callout-info\\"><p>Info | ||
:::</p> | ||
</p>" | ||
`); | ||
}); | ||
|
||
test('should ignore content after callout', () => { | ||
md.use(plugin); | ||
expect( | ||
md.render(dedent` | ||
:::info | ||
Info | ||
::: | ||
Normal paragraph | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"<p class=\\"callout callout-info\\"><p>Info | ||
:::</p> | ||
</p><p>Normal paragraph</p> | ||
" | ||
`); | ||
}); | ||
|
||
test('should ignore content before the callout', () => { | ||
md.use(plugin); | ||
expect( | ||
md.render(dedent` | ||
Normal paragraph | ||
:::info | ||
Info | ||
::: | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"<p>Normal paragraph</p> | ||
<p class=\\"callout callout-info\\"><p>Info | ||
:::</p> | ||
</p>" | ||
`); | ||
}); | ||
|
||
test('should allow nested blocks', () => { | ||
md.use(plugin); | ||
expect( | ||
md.render(dedent` | ||
:::caution | ||
*Alert!* | ||
~~~ | ||
this is my code block | ||
~~~ | ||
::: | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"<p class=\\"callout callout-caution\\"><p><em>Alert!</em></p> | ||
<pre><code>this is my code block | ||
</code></pre> | ||
</p>" | ||
`); | ||
}); | ||
|
||
test('should close tag on EOF', () => { | ||
md.use(plugin); | ||
expect( | ||
md.render(dedent` | ||
:::caution | ||
*Alert!* | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"<p class=\\"callout callout-caution\\"><p><em>Alert!</em></p> | ||
</p>" | ||
`); | ||
}); | ||
|
||
test('should ignore text starting with ":" if not closing tag', () => { | ||
md.use(plugin); | ||
expect( | ||
md.render(dedent` | ||
:::caution | ||
Message | ||
:art: | ||
`) | ||
).toMatchInlineSnapshot(` | ||
"<p class=\\"callout callout-caution\\"><p>Message | ||
:art:</p> | ||
</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 |
---|---|---|
@@ -1,5 +1,60 @@ | ||
import Remarkable from 'remarkable'; | ||
import { TOKENS } from './tokens'; | ||
|
||
export const parser: Remarkable.Plugin = (md, opts) => { | ||
// hello | ||
/** | ||
* Remarkable block parser that recognizes callouts. | ||
* @todo Add options. | ||
*/ | ||
export const parser: Remarkable.BlockParsingRule = ( | ||
state, | ||
startLine, | ||
endLine, | ||
silent | ||
) => { | ||
const pos = state.bMarks[startLine] + state.blkIndent; | ||
const max = state.eMarks[startLine]; | ||
|
||
// Not enough chars or ending line with `:::`. | ||
if (pos + 3 >= max) return false; | ||
|
||
const marker = state.src.charCodeAt(pos); | ||
|
||
// Wrong marker | ||
if (marker !== 0x3a /* ':' */) return false; | ||
|
||
const calloutType = state.src.slice(pos + 3, max).trim(); | ||
|
||
// Scan for marker ending | ||
let nextLine = startLine; | ||
let hasEnding = false; | ||
|
||
while (nextLine < endLine) { | ||
nextLine++; | ||
|
||
const nextPos = state.bMarks[nextLine] + state.tShift[nextLine]; | ||
const nextMax = state.eMarks[nextLine]; | ||
|
||
if (state.src.charCodeAt(nextPos) !== marker) continue; | ||
|
||
const nextLineText = state.src.slice(nextPos, nextMax).trim(); | ||
if (nextLineText === ':::') { | ||
hasEnding = true; | ||
break; | ||
} | ||
} | ||
|
||
// Let register token and progress | ||
state.tokens.push({ | ||
type: TOKENS.CALLOUT_OPEN, | ||
level: state.level++, | ||
lines: [startLine, nextLine + (hasEnding ? 1 : 0)], | ||
calloutType | ||
} as any); | ||
state.parser.tokenize(state, startLine + 1, nextLine); | ||
state.tokens.push({ | ||
type: TOKENS.CALLOUT_CLOSE, | ||
level: state.level-- | ||
} as any); | ||
state.line = nextLine + (hasEnding ? 1 : 0); | ||
return true; | ||
}; |
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,26 @@ | ||
import Remarkable from 'remarkable'; | ||
|
||
/** | ||
* Remarkable callout renderer. | ||
*/ | ||
export const calloutOpenRenderer: Remarkable.Rule = ( | ||
tokens, | ||
idx, | ||
options, | ||
env | ||
) => { | ||
const token = tokens[idx] as any; | ||
return `<p class="callout callout-${token.calloutType}">`; | ||
}; | ||
|
||
/** | ||
* Callout closing tag renderer | ||
*/ | ||
export const calloutCloseRenderer: Remarkable.Rule = ( | ||
tokens, | ||
idx, | ||
options, | ||
env | ||
) => { | ||
return `</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,5 @@ | ||
export const TOKENS = { | ||
CALLOUT: 'callout', | ||
CALLOUT_OPEN: 'callout_open', | ||
CALLOUT_CLOSE: 'callout_close' | ||
}; |
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