Skip to content

Commit

Permalink
feat(plugin): support directive syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v committed Nov 5, 2024
1 parent b8eefa4 commit bf7b4c2
Show file tree
Hide file tree
Showing 7 changed files with 636 additions and 18 deletions.
39 changes: 25 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
"pre-commit": "lint update && lint-staged",
"prepare": "husky"
},
"dependencies": {
"@diplodoc/directive": "file:../diplodoc-directive/diplodoc-directive-0.0.0.tgz"
},
"devDependencies": {
"@diplodoc/lint": "^1.2.0",
"@diplodoc/tsconfig": "^1.0.2",
Expand Down
51 changes: 51 additions & 0 deletions src/plugin/directive.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type MarkdownIt from 'markdown-it';

import {
createBlockInlineToken,
directive,
registerBlockDirective,
tokenizeBlockContent,
} from '@diplodoc/directive';

import {ClassNames, ENV_FLAG_NAME, TokenType} from './const';

export const cutDirective: MarkdownIt.PluginSimple = (md) => {
md.use(directive());

registerBlockDirective(md, 'cut', (state, params) => {
if (!params.content) {
return false;
}

state.env ??= {};
state.env[ENV_FLAG_NAME] = true;

let token = state.push(TokenType.CutOpen, 'details', 1);
token.block = true;
token.attrSet('class', ClassNames.Cut);
token.map = [params.startLine, params.endLine];
token.markup = ':::';

token = state.push(TokenType.TitleOpen, 'summary', 1);
token.attrSet('class', ClassNames.Title);

if (params.inlineContent) {
token = createBlockInlineToken(state, params);
}

token = state.push(TokenType.TitleClose, 'summary', -1);

token = state.push(TokenType.ContentOpen, 'div', 1);
token.attrSet('class', ClassNames.Content);
token.map = [params.startLine + 1, params.endLine - 1];

tokenizeBlockContent(state, params.content, 'yfm-cut');

token = state.push(TokenType.ContentClose, 'div', -1);

token = state.push(TokenType.CutClose, 'details', -1);
token.block = true;

return true;
});
};
1 change: 1 addition & 0 deletions src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const cutPlugin: MarkdownIt.PluginSimple = (md) => {
const newOpenToken = new state.Token(TokenType.CutOpen, 'details', 1);
newOpenToken.attrSet('class', ClassNames.Cut);
newOpenToken.map = tokens[i].map;
newOpenToken.markup = '{%';

attrsParser.apply(newOpenToken);

Expand Down
22 changes: 19 additions & 3 deletions src/plugin/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import type MarkdownIt from 'markdown-it';
import {cutPlugin} from './plugin';
import {type Runtime, copyRuntime, dynrequire, hidden} from './utils';
import {ENV_FLAG_NAME} from './const';
import {cutDirective} from './directive';

type Syntax = 'both' | 'curly' | 'directive';

export type TransformOptions = {
runtime?:
Expand All @@ -12,23 +15,32 @@ export type TransformOptions = {
style: string;
};
bundle?: boolean;
/** @default 'curly' */
syntax?: Syntax;
};

type NormalizedPluginOptions = Omit<TransformOptions, 'runtime'> & {
type NormalizedPluginOptions = Omit<TransformOptions, 'runtime' | 'syntax'> & {
runtime: Runtime;
syntax: Syntax;
};

const registerTransform = (
md: MarkdownIt,
{
runtime,
bundle,
syntax,
output,
}: Pick<NormalizedPluginOptions, 'bundle' | 'runtime'> & {
}: Pick<NormalizedPluginOptions, 'bundle' | 'runtime' | 'syntax'> & {
output: string;
},
) => {
md.use(cutPlugin);
if (syntax === 'curly' || syntax === 'both') {
md.use(cutPlugin);
}
if (syntax === 'directive' || syntax === 'both') {
md.use(cutDirective);
}
md.core.ruler.push('yfm_cut_after', ({env}) => {
hidden(env, 'bundled', new Set<string>());

Expand Down Expand Up @@ -57,6 +69,8 @@ export function transform(options: Partial<TransformOptions> = {}) {
throw new TypeError('Option `runtime` should be record when `bundle` is enabled.');
}

const syntax: Syntax = options.syntax ?? 'curly';

const runtime: Runtime =
typeof options.runtime === 'string'
? {script: options.runtime, style: options.runtime}
Expand All @@ -70,6 +84,7 @@ export function transform(options: Partial<TransformOptions> = {}) {
{output = '.'} = {},
) {
registerTransform(md, {
syntax,
runtime,
bundle,
output,
Expand All @@ -81,6 +96,7 @@ export function transform(options: Partial<TransformOptions> = {}) {
const MdIt = dynrequire('markdown-it');
const md = new MdIt().use((md: MarkdownIt) => {
registerTransform(md, {
syntax,
runtime,
bundle,
output: destRoot,
Expand Down
Loading

0 comments on commit bf7b4c2

Please sign in to comment.