-
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
Showing
1 changed file
with
64 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 @@ | ||
// [[kbd]] | ||
// | ||
|
||
const MARKER_OPEN = '['; | ||
const MARKER_CLOSE = ']'; | ||
const TAG = 'kbd'; | ||
|
||
/* | ||
* Add delimiters for double occurences of MARKER_SYMBOL. | ||
*/ | ||
function tokenize(state, silent) { | ||
if (silent) { | ||
return false; | ||
} | ||
|
||
const start = state.pos; | ||
const max = state.posMax; | ||
let momChar = state.src.charAt(start); | ||
let nextChar = state.src.charAt(start + 1); | ||
|
||
// we're looking for two times the open symbol. | ||
if (momChar !== MARKER_OPEN || nextChar !== MARKER_OPEN) { | ||
return false; | ||
} | ||
|
||
// find the end sequence | ||
let end = -1; | ||
nextChar = state.src.charAt(start + 2); | ||
for (let i = start + 2; i < max && end === -1; i++) { | ||
momChar = nextChar; | ||
nextChar = state.src.charAt(i+1); | ||
if (momChar === MARKER_CLOSE && nextChar === MARKER_CLOSE) { | ||
// found the end! | ||
end = i; | ||
} | ||
if (momChar === MARKER_OPEN && momChar === MARKER_OPEN) { | ||
// found another opening sequence before the end. Thus, ignore ours! | ||
return false; | ||
} | ||
if (momChar === '\n') { | ||
// found end of line before the end sequence. Thus, ignore our start sequence! | ||
return false; | ||
} | ||
} | ||
|
||
// start tag | ||
state.push('kbd', TAG, 1); | ||
// parse inner | ||
state.pos += 2; | ||
state.posMax = end; | ||
state.md.inline.tokenize(state); | ||
state.pos = end + 2; | ||
state.posMax = max; | ||
// end tag | ||
state.push('kbd', TAG, -1); | ||
|
||
return true; | ||
} | ||
|
||
function kbdplugin(markdownit) { | ||
markdownit.inline.ruler.before('link', 'kbd', tokenize); | ||
} | ||
|
||
export default kbdplugin; |