Skip to content

Commit ac81fef

Browse files
authored
Merge pull request mdnice#111 from 375ro/develop
fix: 消除了渲染横屏幻灯片时前后多余的<p>
2 parents cf0a4fa + 2dcec96 commit ac81fef

File tree

1 file changed

+20
-39
lines changed

1 file changed

+20
-39
lines changed

src/utils/markdown-it-imageflow.js

Lines changed: 20 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,53 +6,34 @@ const defaultOption = {
66
const imageFlowPlugin = (md, opt) => {
77
const options = opt || defaultOption;
88

9-
const tokenize = (state, silent) => {
10-
let result = false;
9+
const tokenize = (state, start) => {
1110
let token;
11+
1212
const matchReg = /^<((!\[([^\]])*\]\(([^)])*\)(,?)(\s)*)*)>/;
13+
const srcLine = state.src.slice(state.bMarks[start], state.eMarks[start]);
1314

14-
if (silent) {
15-
return result;
16-
}
17-
if (state.src.charCodeAt(state.pos) !== 0x3c /* < */) {
18-
return result;
15+
if (srcLine.charCodeAt(0) !== 0x3c /* < */) {
16+
return false;
1917
}
20-
21-
const match = matchReg.exec(state.src.substr(state.pos));
18+
const match = matchReg.exec(srcLine);
2219

2320
if (match) {
24-
if (options.limitless) {
25-
result = true;
26-
} else if (match[1].split(/,|\s/).filter((val) => val).length < options.limit) {
27-
result = true;
28-
} else {
29-
return result;
30-
}
31-
32-
token = state.push("imageFlow_start", "imageFlow", 1);
33-
token = state.push("imageFlow_content", "imageFlow", 0);
34-
[, token.content] = match;
35-
token = state.push("imageFlow_end", "imageFlow", -1);
36-
37-
// update position
38-
var newline = state.src.indexOf("\n", state.pos);
39-
if (newline !== -1) {
40-
state.pos = newline;
41-
} else {
42-
state.pos = state.pos + state.posMax + 1;
21+
if (!options.limitless && match[1].split(/,|\s/).filter((val) => val).length < options.limit) {
22+
token = state.push("imageFlow", "", 0);
23+
[, token.content] = match;
24+
token.block = true;
25+
26+
// update line
27+
state.line++;
28+
return true;
4329
}
4430
}
45-
46-
return result;
31+
return false;
4732
};
4833

49-
md.renderer.rules.imageFlow_start = () => {
50-
return `<section class="imageflow-layer1"><section class="imageflow-layer2">`;
51-
};
52-
md.renderer.rules.imageFlow_end = () => {
53-
return `</section></section>`;
54-
};
55-
md.renderer.rules.imageFlow_content = (tokens, idx) => {
34+
md.renderer.rules.imageFlow = (tokens, idx) => {
35+
const start = `<section class="imageflow-layer1"><section class="imageflow-layer2">`;
36+
const end = `</section></section>`;
5637
const contents = tokens[idx].content.split(/,|\s/).filter((val) => val);
5738
let wrapperContent = "";
5839
let image;
@@ -63,10 +44,10 @@ const imageFlowPlugin = (md, opt) => {
6344
} class="imageflow-img" /></section>`;
6445
});
6546

66-
return wrapperContent;
47+
return start + wrapperContent + end;
6748
};
6849

69-
md.inline.ruler.before("image", "imageFlow", tokenize);
50+
md.block.ruler.before("paragraph", "imageFlow", tokenize);
7051
};
7152

7253
export default imageFlowPlugin;

0 commit comments

Comments
 (0)