Skip to content

Commit

Permalink
feat(plugin): upd deps and replace option with option
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m1d0v committed Nov 13, 2024
1 parent 4efae92 commit edb51bc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 35 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 @@ -47,7 +47,7 @@
"prepare": "husky"
},
"dependencies": {
"@diplodoc/directive": "file:../diplodoc-directive/diplodoc-directive-0.0.0-8.tgz"
"@diplodoc/directive": "^0.1.0"
},
"devDependencies": {
"@diplodoc/lint": "^1.2.0",
Expand Down
33 changes: 19 additions & 14 deletions src/plugin/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ 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?:
| string
Expand All @@ -15,30 +13,38 @@ export type TransformOptions = {
style: string;
};
bundle?: boolean;
/** @default 'curly' */
syntax?: Syntax;
/**
* Enables directive syntax of yfm-cut.
*
* - 'disabled' – directive syntax is disabled.
* - 'enabled' – directive syntax is enabled; old syntax is also enabled.
* - 'only' – enabled only directive syntax; old syntax is disabled.
*
* @default 'disabled'
*/
directiveSyntax?: 'disabled' | 'enabled' | 'only';
};

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

const registerTransform = (
md: MarkdownIt,
{
runtime,
bundle,
syntax,
output,
}: Pick<NormalizedPluginOptions, 'bundle' | 'runtime' | 'syntax'> & {
directiveSyntax,
}: Pick<NormalizedPluginOptions, 'bundle' | 'runtime' | 'directiveSyntax'> & {
output: string;
},
) => {
if (syntax === 'curly' || syntax === 'both') {
if (directiveSyntax === 'disabled' || directiveSyntax === 'enabled') {
md.use(cutPlugin);
}
if (syntax === 'directive' || syntax === 'both') {
if (directiveSyntax === 'enabled' || directiveSyntax === 'only') {
md.use(cutDirective);
}
md.core.ruler.push('yfm_cut_after', ({env}) => {
Expand Down Expand Up @@ -69,8 +75,7 @@ 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 directiveSyntax = options.directiveSyntax || 'disabled';
const runtime: Runtime =
typeof options.runtime === 'string'
? {script: options.runtime, style: options.runtime}
Expand All @@ -84,7 +89,7 @@ export function transform(options: Partial<TransformOptions> = {}) {
{output = '.'} = {},
) {
registerTransform(md, {
syntax,
directiveSyntax,
runtime,
bundle,
output,
Expand All @@ -96,7 +101,7 @@ export function transform(options: Partial<TransformOptions> = {}) {
const MdIt = dynrequire('markdown-it');
const md = new MdIt().use((md: MarkdownIt) => {
registerTransform(md, {
syntax,
directiveSyntax,
runtime,
bundle,
output: destRoot,
Expand Down
6 changes: 3 additions & 3 deletions tests/src/__snapshots__/plugin.test.ts.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Cut extension - plugin Options: syntax should render both cuts 1`] = `
exports[`Cut extension - plugin Options: directiveSyntax should render both cuts 1`] = `
<details class="yfm-cut">
<summary class="yfm-cut-title">
Old cut
Expand All @@ -23,7 +23,7 @@ exports[`Cut extension - plugin Options: syntax should render both cuts 1`] = `
</details>
`;

exports[`Cut extension - plugin Options: syntax should render only new (directive) cut 1`] = `
exports[`Cut extension - plugin Options: directiveSyntax should render only new (directive) cut 1`] = `
<p>
</p>
<p>
Expand All @@ -43,7 +43,7 @@ exports[`Cut extension - plugin Options: syntax should render only new (directiv
</details>
`;

exports[`Cut extension - plugin Options: syntax should render only old (curly) cut 1`] = `
exports[`Cut extension - plugin Options: directiveSyntax should render only old (curly) cut 1`] = `
<details class="yfm-cut">
<summary class="yfm-cut-title">
Old cut
Expand Down
26 changes: 13 additions & 13 deletions tests/src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ describe('Cut extension - plugin', () => {
Cut content
:::
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toMatchSnapshot();
});
Expand All @@ -236,7 +236,7 @@ describe('Cut extension - plugin', () => {
:::
:::
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toMatchSnapshot();
});
Expand All @@ -249,13 +249,13 @@ describe('Cut extension - plugin', () => {
Content we want to hide
:::
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toMatchSnapshot();
});

it('should dont add assets to meta if no yfm-cut is found', () => {
expect(meta('paragraph', {syntax: 'directive'})).toBeUndefined();
expect(meta('paragraph', {directiveSyntax: 'only'})).toBeUndefined();
});

it('should add assets to meta', () => {
Expand All @@ -266,7 +266,7 @@ describe('Cut extension - plugin', () => {
Cut content
:::
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toStrictEqual({
script: ['_assets/cut-extension.js'],
Expand All @@ -286,7 +286,7 @@ describe('Cut extension - plugin', () => {
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toMatchSnapshot();
});
Expand All @@ -298,7 +298,7 @@ describe('Cut extension - plugin', () => {
:::cut
Cut content
:::`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toMatchSnapshot();
});
Expand All @@ -309,7 +309,7 @@ describe('Cut extension - plugin', () => {
dd`
::cut[Title]
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toMatchSnapshot();
});
Expand All @@ -320,13 +320,13 @@ describe('Cut extension - plugin', () => {
dd`
::cut[Title]
`,
{syntax: 'directive'},
{directiveSyntax: 'only'},
),
).toBeUndefined();
});
});

describe('Options: syntax', () => {
describe('Options: directiveSyntax', () => {
const markup = dd`
{% cut "Old cut" %}
Expand All @@ -340,15 +340,15 @@ describe('Cut extension - plugin', () => {
`;

it('should render only old (curly) cut', () => {
expect(html(markup, {syntax: 'curly'})).toMatchSnapshot();
expect(html(markup, {directiveSyntax: 'disabled'})).toMatchSnapshot();
});

it('should render only new (directive) cut', () => {
expect(html(markup, {syntax: 'directive'})).toMatchSnapshot();
expect(html(markup, {directiveSyntax: 'only'})).toMatchSnapshot();
});

it('should render both cuts', () => {
expect(html(markup, {syntax: 'both'})).toMatchSnapshot();
expect(html(markup, {directiveSyntax: 'enabled'})).toMatchSnapshot();
});
});
});

0 comments on commit edb51bc

Please sign in to comment.