Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin): support directive syntax #22

Merged
merged 6 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) => {
d3m1d0v marked this conversation as resolved.
Show resolved Hide resolved
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
23 changes: 23 additions & 0 deletions tests/package-lock.json

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

4 changes: 3 additions & 1 deletion tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "@diplodoc/tests",
"private": true,
"scripts": {
"test": "jest"
"test": "jest",
"test:watch": "jest --watch"
},
"devDependencies": {
"@diplodoc/transform": "^4.28.2",
"@types/jest": "^29.5.12",
"@types/markdown-it": "^13.0.9",
"esbuild-jest": "^0.5.0",
"highlight.js": "^11.10.0",
"jest": "^29.7.0",
Expand Down
Loading
Loading