Skip to content

Commit

Permalink
feat: replace cut with an extension from separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v authored and v8tenko committed Sep 5, 2024
1 parent 259e426 commit 9964674
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 156 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
},
"dependencies": {
"@diplodoc/tabs-extension": "^3.3.0",
"@diplodoc/cut-extension": "^0.1.0",
"chalk": "^4.1.2",
"cheerio": "^1.0.0-rc.12",
"css": "^3.0.0",
Expand Down
57 changes: 0 additions & 57 deletions src/js/cut.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '@diplodoc/cut-extension/runtime';
import '@diplodoc/tabs-extension/runtime';

import './polyfill';
import './code';
import './cut';
import './term';
import './wide-mode';

Expand Down
2 changes: 1 addition & 1 deletion src/scss/yfm.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
@import 'anchor';
@import 'highlight';
@import 'code';
@import 'cut';
@import 'file';
@import 'term';
@import 'table';
@import 'modal';

@import '@diplodoc/cut-extension/runtime/styles.css';
@import '@diplodoc/tabs-extension/runtime';
99 changes: 2 additions & 97 deletions src/transform/plugins/cut.ts
Original file line number Diff line number Diff line change
@@ -1,98 +1,3 @@
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';
import {transform} from '@diplodoc/cut-extension';

const CUT_REGEXP = /^{%\s*cut\s*["|'](.*)["|']\s*%}/;

const matchCloseToken: MatchTokenFunction = (tokens, i) => {
return (
tokens[i].type === 'paragraph_open' &&
tokens[i + 1].type === 'inline' &&
tokens[i + 1].content.trim() === '{% endcut %}'
);
};

const matchOpenToken = (tokens: Token[], i: number) => {
return (
tokens[i].type === 'paragraph_open' &&
tokens[i + 1].type === 'inline' &&
tokens[i + 1].content.match(CUT_REGEXP)
);
};

const findCloseTokenIdx = closeTokenFactory('Cut', matchOpenToken, matchCloseToken);

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

while (i < tokens.length) {
const match = matchOpenToken(tokens, i);

if (match) {
const closeTokenIdx = findCloseTokenIdx(tokens, i + 4, path, log);

if (!closeTokenIdx) {
i += 3;
continue;
}

const newOpenToken = new state.Token('yfm_cut_open', 'details', 1);
newOpenToken.attrSet('class', 'yfm-cut');
newOpenToken.map = tokens[i].map;

const titleOpen = new state.Token('yfm_cut_title_open', 'summary', 1);
titleOpen.attrSet('class', 'yfm-cut-title');

const titleInline = state.md.parseInline(
match[1] === undefined ? 'ad' : match[1],
state.env,
)[0];

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

const contentOpen = new state.Token('yfm_cut_content_open', 'div', 1);
contentOpen.attrSet('class', 'yfm-cut-content');

if (newOpenToken.map) {
const contentOpenStart = newOpenToken.map[0] + 1;
const contentOpenEnd = newOpenToken.map[0] + 2;

contentOpen.map = [contentOpenStart, contentOpenEnd];
}

const contentClose = new state.Token('yfm_cut_content_close', 'div', -1);

const newCloseToken = new state.Token('yfm_cut_close', 'details', -1);
newCloseToken.map = tokens[closeTokenIdx].map;

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

tokens.splice(i, closeTokenIdx - i + 3, ...insideTokens);

i++;
} else {
i++;
}
}
};

try {
md.core.ruler.before('curly_attributes', 'cut', plugin);
} catch (e) {
md.core.ruler.push('cut', plugin);
}
};

export = cut;
export = transform({bundle: false});

0 comments on commit 9964674

Please sign in to comment.