Skip to content

Commit

Permalink
add front prior mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Oct 16, 2019
1 parent 7d6d150 commit 6d04f43
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
11 changes: 11 additions & 0 deletions README.md
Expand Up @@ -2,3 +2,14 @@
> `!!spoiler!!` plugin for markdown-it markdown parser
`!!spoiler!!` => `<span class="spoiler">spoiler</span>`

## `frontPriorMode`(default: `false`)
### when `false`

`!!!spoiler!!` => `!<span class="spoiler">spoiler</span>`
`!!spoiler!!!` => `<span class="spoiler">spoiler!</span>`

### when `true`

`!!!spolier!!` => `<span class="spoiler">!spoiler</span>`
`!!spolier!!!` => `<span class="spoiler">spoiler</span>!`
17 changes: 12 additions & 5 deletions index.js
Expand Up @@ -26,7 +26,7 @@
'use strict'
const exMark = 0x21 /* ! */

const tokenize = (state, silent) => {
const tokenize = frontPriorMode => (state, silent) => {
if (silent) return false

const start = state.pos
Expand All @@ -40,9 +40,13 @@ const tokenize = (state, silent) => {

if (len < 2) return false

let isOdd = false
if (len % 2) {
const token = state.push("text", "", 0)
token.content = ch
isOdd = true
if (!frontPriorMode) {
const token = state.push("text", "", 0)
token.content = ch
}
len--
}

Expand All @@ -62,6 +66,9 @@ const tokenize = (state, silent) => {
}

state.pos += scanned.length
if (isOdd && frontPriorMode) {
state.pos--
}

return true
}
Expand Down Expand Up @@ -125,8 +132,8 @@ const postProcess = (state, delimiters) => {
}
}

module.exports = function(md) {
md.inline.ruler.before("emphasis", "spoiler", tokenize)
module.exports = function(md, frontPriorMode = false) {
md.inline.ruler.before("emphasis", "spoiler", tokenize(frontPriorMode))
md.inline.ruler2.before("emphasis", "spoiler", state => {
postProcess(state, state.delimiters)

Expand Down

0 comments on commit 6d04f43

Please sign in to comment.