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: support render flags #70

Merged
merged 1 commit into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ export enum TabsVariants {
Accordion = 'accordion',
}

export type EnabledVariants = Partial<Record<TabsVariants, boolean>>;

export interface Tab {
group?: string;
key: string;
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function props(content: string): TabsProps {
const props = clean.split(' ');
const result: TabsProps = {
content: clean,
orientation: TabsVariants.Regular,
variant: TabsVariants.Regular,
group: `${DEFAULT_TABS_GROUP_PREFIX}${generateID()}`,
};

Expand All @@ -58,7 +58,7 @@ export function props(content: string): TabsProps {
case 'radio':
case 'dropdown':
case 'accordion':
result.orientation = key as TabsVariants;
result.variant = key as TabsVariants;
break;
case 'group':
result.group = unquote(value);
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function generateTabsTokens(
state: StateCore,
props: TabsGenerationProps,
) {
const tokens = generateTokensByType(props.orientation)(tabs, state, props);
const tokens = generateTokensByType(props.variant)(tabs, state, props);

return tokens;
}
22 changes: 19 additions & 3 deletions src/plugin/transform.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type StateCore from 'markdown-it/lib/rules_core/state_core';
import type MarkdownIt from 'markdown-it';

import {EnabledVariants, TabsVariants} from '../common';

import {addHiddenProperty, copyRuntimeFiles} from './utils';
import {generateTabsTokens} from './generate';
import {findTabs, props, tryToFindTabs} from './find';
Expand All @@ -10,8 +12,17 @@ export type PluginOptions = {
runtimeCssPath: string;
containerClasses: string;
bundle: boolean;
features: {
enabledVariants: EnabledVariants;
};
};

const defaultFeatures = {
enabledVariants: {
regular: true,
},
} satisfies PluginOptions['features'];

let runsCounter = 0;

type TransformOptions = {
Expand All @@ -23,6 +34,7 @@ export function transform({
runtimeCssPath = '_assets/tabs-extension.css',
containerClasses = '',
bundle = true,
features = defaultFeatures,
}: Partial<PluginOptions> = {}) {
return function tabs(md: MarkdownIt, options?: TransformOptions) {
const {output = '.'} = options || {};
Expand All @@ -46,15 +58,19 @@ export function transform({

const {content, closeTokenIndex} = result;

const {group, orientation} = props(content);
const parsedProps = props(content);

if (!features.enabledVariants[parsedProps.variant]) {
parsedProps.variant = TabsVariants.Regular;
}

const tabs = findTabs(state.tokens, i + 3, closeTokenIndex);

if (tabs.length > 0) {
const tabsTokens = generateTabsTokens(tabs, state, {
containerClasses,
tabsGroup: group,
orientation,
tabsGroup: parsedProps.group,
variant: parsedProps.variant,
runId,
});

Expand Down
2 changes: 1 addition & 1 deletion src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export type RuntimeTab = {

export type TabsProps = {
content: string;
orientation: TabsVariants;
variant: TabsVariants;
group: string;
};
2 changes: 1 addition & 1 deletion src/plugin/variants/regular.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {TabsTokensGenerator} from './types';
export const regular: TabsTokensGenerator = (
tabs,
state,
{containerClasses, tabsGroup, runId, orientation},
{containerClasses, tabsGroup, runId, variant: orientation},
) => {
const tabsTokens = [];
const tabListTokens = [];
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/variants/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type TabsGenerationProps = {
containerClasses: string;
tabsGroup: string;
runId: string;
orientation: TabsVariants;
variant: TabsVariants;
};

export type TabsTokensGenerator = (
Expand Down
14 changes: 12 additions & 2 deletions tests/src/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,25 @@ function makeTransform(params?: {
content?: string[];
}) {
return callPlugin(
tabsExtension.transform({bundle: false, ...params?.transformOptions}),
tabsExtension.transform({
bundle: false,
features: {enabledVariants: {radio: true, regular: true}},
...params?.transformOptions,
}),
tokenize(params?.content || defaultContent),
);
}

function html(text: string, opts?: tabsExtension.PluginOptions) {
const {result} = transform(text, {
needToSanitizeHtml: false,
plugins: [tabsExtension.transform({bundle: false, ...opts})],
plugins: [
tabsExtension.transform({
bundle: false,
features: {enabledVariants: {radio: true, regular: true}},
...opts,
}),
],
});

return result.html;
Expand Down
Loading