Skip to content

Commit

Permalink
fix: revert pr338 && add cut w/ code test (#358)
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill Omelianenko
  • Loading branch information
Kiri111enz authored Dec 27, 2023
1 parent 0114447 commit b5449c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 1 addition & 5 deletions src/transform/plugins/cut.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,15 @@ const cut: MarkdownItPluginCb = (md, {path, log}) => {
const newCloseToken = new state.Token('yfm_cut_close', 'div', -1);
newCloseToken.map = tokens[closeTokenIdx].map;

const insertTokens = tokens.slice(i + 3, closeTokenIdx);
const rest = insertTokens.length % 3;

const insideTokens = [
newOpenToken,
titleOpen,
titleInline,
titleClose,
contentOpen,
...insertTokens.slice(0, insertTokens.length - rest),
...tokens.slice(i + 3, closeTokenIdx),
contentClose,
newCloseToken,
...insertTokens.slice(insertTokens.length - rest),
];

tokens.splice(i, closeTokenIdx - i + 3, ...insideTokens);
Expand Down
18 changes: 18 additions & 0 deletions test/cut.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ describe('Cut plugin', () => {
);
});

it('should render simple cut with code in it', () => {
expect(
transformYfm(
'{% cut "Cut title" %}\n' +
'\n' +
'```\n' +
'Code\n' +
'```\n' +
'\n' +
'{% endcut %}',
).replace(/(\r\n|\n|\r)/gm, ''),
).toBe(
'<div class="yfm-cut"><div class="yfm-cut-title">Cut title</div>' +
'<div class="yfm-cut-content"><pre><code class="hljs">Code</code></pre>' +
'</div></div>',
);
});

it('should render siblings cuts', () => {
expect(
transformYfm(
Expand Down

0 comments on commit b5449c2

Please sign in to comment.