Skip to content

Commit 5e6a24b

Browse files
authored
Merge pull request #167 from d3m1d0v/cut-title-format
feat(cut): allow inline formatting in cut title
2 parents d5cc618 + c281878 commit 5e6a24b

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

src/transform/plugins/cut.ts

+13-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import StateCore from 'markdown-it/lib/rules_core/state_core';
2-
import Token from 'markdown-it/lib/token';
1+
import type Core from 'markdown-it/lib/parser_core';
2+
import type Token from 'markdown-it/lib/token';
33
import {MarkdownItPluginCb} from './typings';
44
import {MatchTokenFunction, nestedCloseTokenIdxFactory as closeTokenFactory} from './utils';
55

@@ -24,7 +24,7 @@ const matchOpenToken = (tokens: Token[], i: number) => {
2424
const findCloseTokenIdx = closeTokenFactory('Cut', matchOpenToken, matchCloseToken);
2525

2626
const cut: MarkdownItPluginCb = (md, {path, log}) => {
27-
const plugin = (state: StateCore) => {
27+
const plugin: Core.RuleCore = (state) => {
2828
const tokens = state.tokens;
2929
let i = 0;
3030

@@ -45,8 +45,15 @@ const cut: MarkdownItPluginCb = (md, {path, log}) => {
4545
const titleOpen = new state.Token('yfm_cut_title_open', 'div', 1);
4646
titleOpen.attrSet('class', 'yfm-cut-title');
4747

48-
const textTitle = new state.Token('text', '', 0);
49-
textTitle.content = match[1] === undefined ? 'ad' : match[1];
48+
const titleInline = new state.Token('inline', '', 0);
49+
titleInline.content = match[1] === undefined ? 'ad' : match[1];
50+
titleInline.children = [];
51+
state.md.inline.parse(
52+
titleInline.content,
53+
state.md,
54+
state.env,
55+
titleInline.children,
56+
);
5057

5158
const titleClose = new state.Token('yfm_cut_title_close', 'div', -1);
5259

@@ -60,7 +67,7 @@ const cut: MarkdownItPluginCb = (md, {path, log}) => {
6067
const insideTokens = [
6168
newOpenToken,
6269
titleOpen,
63-
textTitle,
70+
titleInline,
6471
titleClose,
6572
contentOpen,
6673
...tokens.slice(i + 3, closeTokenIdx),

test/cut.test.ts

+17
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,21 @@ describe('Cut plugin', () => {
6969
'</div></div></div>',
7070
);
7171
});
72+
73+
it('should render title with format', () => {
74+
expect(
75+
transformYfm(
76+
'{% cut "**Strong cut title**" %}\n' +
77+
'\n' +
78+
'Content we want to hide\n' +
79+
'\n' +
80+
'{% endcut %}',
81+
),
82+
).toBe(
83+
'<div class="yfm-cut">' +
84+
'<div class="yfm-cut-title"><strong>Strong cut title</strong></div>' +
85+
'<div class="yfm-cut-content"><p>Content we want to hide</p>\n</div>' +
86+
'</div>',
87+
);
88+
});
7289
});

0 commit comments

Comments
 (0)