Skip to content

Commit

Permalink
Merge pull request #167 from d3m1d0v/cut-title-format
Browse files Browse the repository at this point in the history
feat(cut): allow inline formatting in cut title
  • Loading branch information
d3m1d0v authored Aug 18, 2022
2 parents d5cc618 + c281878 commit 5e6a24b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
19 changes: 13 additions & 6 deletions src/transform/plugins/cut.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import StateCore from 'markdown-it/lib/rules_core/state_core';
import Token from 'markdown-it/lib/token';
import type Core from 'markdown-it/lib/parser_core';
import type Token from 'markdown-it/lib/token';
import {MarkdownItPluginCb} from './typings';
import {MatchTokenFunction, nestedCloseTokenIdxFactory as closeTokenFactory} from './utils';

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

const cut: MarkdownItPluginCb = (md, {path, log}) => {
const plugin = (state: StateCore) => {
const plugin: Core.RuleCore = (state) => {
const tokens = state.tokens;
let i = 0;

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

const textTitle = new state.Token('text', '', 0);
textTitle.content = match[1] === undefined ? 'ad' : match[1];
const titleInline = new state.Token('inline', '', 0);
titleInline.content = match[1] === undefined ? 'ad' : match[1];
titleInline.children = [];
state.md.inline.parse(
titleInline.content,
state.md,
state.env,
titleInline.children,
);

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

Expand All @@ -60,7 +67,7 @@ const cut: MarkdownItPluginCb = (md, {path, log}) => {
const insideTokens = [
newOpenToken,
titleOpen,
textTitle,
titleInline,
titleClose,
contentOpen,
...tokens.slice(i + 3, closeTokenIdx),
Expand Down
17 changes: 17 additions & 0 deletions test/cut.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,21 @@ describe('Cut plugin', () => {
'</div></div></div>',
);
});

it('should render title with format', () => {
expect(
transformYfm(
'{% cut "**Strong cut title**" %}\n' +
'\n' +
'Content we want to hide\n' +
'\n' +
'{% endcut %}',
),
).toBe(
'<div class="yfm-cut">' +
'<div class="yfm-cut-title"><strong>Strong cut title</strong></div>' +
'<div class="yfm-cut-content"><p>Content we want to hide</p>\n</div>' +
'</div>',
);
});
});

0 comments on commit 5e6a24b

Please sign in to comment.