Skip to content

Commit

Permalink
feat: support render flags
Browse files Browse the repository at this point in the history
  • Loading branch information
v8tenko committed Oct 17, 2024
1 parent a4537c3 commit dff6b60
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
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;
}
18 changes: 15 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,6 +12,11 @@ export type PluginOptions = {
runtimeCssPath: string;
containerClasses: string;
bundle: boolean;
enabledVariants: EnabledVariants;
};

const defaultEnabledVariants: EnabledVariants = {
regular: true,
};

let runsCounter = 0;
Expand All @@ -23,6 +30,7 @@ export function transform({
runtimeCssPath = '_assets/tabs-extension.css',
containerClasses = '',
bundle = true,
enabledVariants = defaultEnabledVariants,
}: Partial<PluginOptions> = {}) {
return function tabs(md: MarkdownIt, options?: TransformOptions) {
const {output = '.'} = options || {};
Expand All @@ -46,15 +54,19 @@ export function transform({

const {content, closeTokenIndex} = result;

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

if (!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

0 comments on commit dff6b60

Please sign in to comment.