add inline block support
fix infinite loop
This commit is contained in:
parent
f6d63781f0
commit
3e2a375aea
5 changed files with 58 additions and 18 deletions
12
README.md
12
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:
|
|||
<p><em class="a">x</em> <strong class="b">y</strong> <em class="c"><strong>z</strong></em></p>
|
||||
```
|
||||
|
||||
### 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:
|
||||
|
|
17
lib/index.js
17
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;
|
||||
}
|
||||
|
|
19
lib/rules.js
19
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;
|
||||
},
|
||||
},
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -89,3 +89,26 @@ line2
|
|||
line2
|
||||
</code></pre>
|
||||
.
|
||||
|
||||
|
||||
ver 1.0.0 bug: should not be trapped in infinite loop
|
||||
.
|
||||
- a: [b}{.c}d
|
||||
.
|
||||
<ul>
|
||||
<li>a: [b}{.c}d</li>
|
||||
</ul>
|
||||
.
|
||||
|
||||
|
||||
`code` block
|
||||
.
|
||||
a `b`{.c} d
|
||||
|
||||
- `e`{.f} g
|
||||
.
|
||||
<p>a <code class="c">b</code> d</p>
|
||||
<ul>
|
||||
<li><code class="f">e</code> g</li>
|
||||
</ul>
|
||||
.
|
||||
|
|
Reference in a new issue