Skip to content

Commit

Permalink
feat(plugin): use config-object to register directive
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v authored and makhnatkin committed Nov 15, 2024
1 parent 0d2cf53 commit d853d00
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 39 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@diplodoc/directive": "^0.1.0"
"@diplodoc/directive": "^0.2.0"
}
}
60 changes: 26 additions & 34 deletions src/plugin/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,45 +60,38 @@ type RegisterTransformOptions = Pick<
'runtimeJsPath' | 'bundle' | 'embeddingMode'
> & {
output: string;
updateTokens: boolean;
};

const registerTransform = (
md: MarkdownIt,
{embeddingMode, runtimeJsPath, output, bundle, updateTokens}: RegisterTransformOptions,
{embeddingMode, runtimeJsPath, output, bundle}: RegisterTransformOptions,
) => {
md.use(directiveParser());

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

if (updateTokens) {
const tokenType = embeddingModeToTokenType[embeddingMode];
const token = state.push(tokenType, TAG, 0);
// set fields like in fence token
token.map = [params.startLine, params.endLine];
token.content = params.content.raw;
token.markup = ':::';
token.info = 'html';
}

const {env} = state;
env.meta ||= {};
env.meta.script ||= [];
env.meta.style ||= [];
if (!env.meta.script.includes(runtimeJsPath)) {
env.meta.script.push(runtimeJsPath);
}

addHiddenProperty(env, 'bundled', new Set<string>());

if (bundle) {
copyRuntimeFiles({runtimeJsPath, output}, env.bundled);
}

return true;
registerContainerDirective(md, {
name: 'html',
type: 'code_block',
container: {
tag: TAG,
token: embeddingModeToTokenType[embeddingMode],
},
match: (_params, state) => {
const {env} = state;
env.meta ||= {};
env.meta.script ||= [];
env.meta.style ||= [];
if (!env.meta.script.includes(runtimeJsPath)) {
env.meta.script.push(runtimeJsPath);
}

addHiddenProperty(env, 'bundled', new Set<string>());

if (bundle) {
copyRuntimeFiles({runtimeJsPath, output}, env.bundled);
}

return true;
},
});
};

Expand All @@ -116,7 +109,7 @@ export function transform({
const plugin: MarkdownIt.PluginWithOptions<TransformOptions> = (md, options) => {
const {output = '.'} = options || {};

registerTransform(md, {embeddingMode, runtimeJsPath, bundle, output, updateTokens: true});
registerTransform(md, {embeddingMode, runtimeJsPath, bundle, output});

md.renderer.rules[SRCDOC_TOKEN_TYPE] = makeSrcdocModeEmbedRenderRule({
containerClassNames: containerClasses,
Expand Down Expand Up @@ -163,7 +156,6 @@ export function transform({
runtimeJsPath,
bundle,
output: destRoot,
updateTokens: false,
});
});

Expand Down

0 comments on commit d853d00

Please sign in to comment.