Skip to content

Commit

Permalink
+ index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jGleitz committed Nov 6, 2016
1 parent 89ec4f8 commit 5df911d
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions index.js
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;

0 comments on commit 5df911d

Please sign in to comment.