diff --git a/README.md b/README.md index 3a29677..194e1a6 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,16 @@ to write id, classes, and attributes. ### Where to put `{...}` |type|where|example| |:-:|:-:|--| -|inline tag|**AFTER** tag|em / strong| -|inline block|beginning / end|li / td / th| +|inline block|**AFTER** tag|em / strong / code| +|inline container|beginning / end|li / td / th| |block|**BEFORE** block|h1 / ul / table| Note: There is no way to add attributes to `tr` without extension. See [Extension: Attributes for tr](#tr-extension) for more info. ## Examples -### Attributes for inline tag -Add `{...}` **AFTER** the inline tag. +### Attributes for inline block +Add `{...}` **AFTER** the inline block. #### em / strong Example Input: @@ -29,8 +29,8 @@ Output:
x y z
``` -### Attributes for inline block -Add `{...}` at the **beginning** or the **end** in the inline block. +### Attributes for inline container +Add `{...}` at the **beginning** or the **end** in the inline container. #### list item Example Input: diff --git a/lib/index.js b/lib/index.js index 48cee12..854960b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -36,6 +36,7 @@ function pluginAttr(md, opts={}) { // should meet end of line if (!(attrs && index === indexEnd)) return false; // push + // [block] silent: no need to update state if (silent) return true; state.line = l0+1; const token = state.push('block_attr', '', 0); @@ -53,11 +54,17 @@ function pluginAttr(md, opts={}) { const {attrs, index} = parseAttrs(src, re) || {}; if (!attrs) return false; // set attr - if (silent) return true; - const token = state.push('inline_attr', '', 0); - token.attrs = attrs; - token.content = src.substring(pos, index); - token.hidden = true; + /* + [inline] silent: + false -> push token and update state + true -> set state.pos only + */ + if (!silent) { + const token = state.push('inline_attr', '', 0); + token.attrs = attrs; + token.content = src.substring(pos, index); + token.hidden = true; + } state.pos = index; return true; } diff --git a/lib/rules.js b/lib/rules.js index 55dbab7..a7433b0 100644 --- a/lib/rules.js +++ b/lib/rules.js @@ -21,15 +21,22 @@ const inlineAttrsApplyRules = [ }, }, { - name: 'after_inline_close', + name: 'after_inline_block', handler(token, i, tokens, iBlock, blockTokens) { + // find non-empty text token const iC = rfindIndex(tokens, i-1, t=>t.type!=='text'||t.content); const tokenC = iC>=0 && tokens[iC]; - if (!(tokenC && tokenC.nesting === -1)) return; - // find open - const tokenO = findOpenToken(tokens, iC); - if (!tokenO) return false; - attrConcat(tokenO, token); + if (!tokenC) return; + // check tokenC type + if (tokenC.nesting === -1) { + // close token -> find open and apply + const tokenO = findOpenToken(tokens, iC); + if (!tokenO) return false; + attrConcat(tokenO, token); + } else if (tokenC.nesting === 0 && tokenC.type !== 'text') { + // block token -> apply directly + attrConcat(tokenC, token); + } else return false; // not inline block return true; }, }, diff --git a/package.json b/package.json index 45debdf..9a3a642 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sup39/markdown-it-attr", - "version": "1.0.0", + "version": "1.1.0", "description": "A markdown-it plugin to write id, classes, and attributes", "keywords": [ "markdown", @@ -15,6 +15,9 @@ "lint": "eslint .", "test": "mocha" }, + "files": [ + "lib/" + ], "devDependencies": { "@sup39/markdown-it-raw-table": "^1.0.0", "eslint": "^7.17.0", diff --git a/test/cases/common.txt b/test/cases/common.txt index fb634a3..2446ebc 100644 --- a/test/cases/common.txt +++ b/test/cases/common.txt @@ -89,3 +89,26 @@ line2 line2 . + + +ver 1.0.0 bug: should not be trapped in infinite loop +. +- a: [b}{.c}d +. +a b
d
e
g