1
0
Fork 0

add inline block support

fix infinite loop
This commit is contained in:
sup39 2021-01-14 19:06:02 +09:00
parent f6d63781f0
commit 3e2a375aea
5 changed files with 58 additions and 18 deletions

View file

@ -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:

View file

@ -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;
}

View file

@ -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;
},
},

View file

@ -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",

View file

@ -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>
.