Skip to content

Commit

Permalink
feat: toc label syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Feverqwe authored and 3y3 committed Mar 25, 2024
1 parent 35d2b9c commit 45e118f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ export type DocPreset = {
__metadata: Record<string, string>[];
};

export interface YfmTocLabel extends Filter {
title: string;
description?: string;
theme?: string;
}

export interface YfmToc extends Filter {
name: string;
href: string;
Expand All @@ -89,6 +95,7 @@ export interface YfmToc extends Filter {
id?: string;
singlePage?: boolean;
hidden?: boolean;
label?: YfmTocLabel[] | YfmTocLabel;
}

export interface YfmTocInclude {
Expand Down
8 changes: 7 additions & 1 deletion src/services/tocs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {getContentWithUpdatedStaticMetadata} from './metadata';
import {YfmToc} from '../models';
import {IncludeMode, Stage} from '../constants';
import {isExternalHref, logger} from '../utils';
import {filterFiles, firstFilterTextItems, liquidField} from './utils';
import {filterFiles, firstFilterItem, firstFilterTextItems, liquidField} from './utils';
import {IncludersError, applyIncluders} from './includers';

export interface TocServiceData {
Expand Down Expand Up @@ -59,6 +59,12 @@ async function add(path: string) {
parsedToc.title = liquidField(parsedToc.title, combinedVars, path);
}

if (parsedToc.label) {
parsedToc.label = firstFilterItem(parsedToc.label, combinedVars, {
resolveConditions: true,
});
}

parsedToc.items = await processTocItems(
path,
parsedToc.items,
Expand Down
20 changes: 20 additions & 0 deletions src/services/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ export function firstFilterTextItems(
return filteredItems[0] || '';
}

export function firstFilterItem<T extends Filter>(
itemOrItems: T | T[],
vars: Record<string, string>,
options?: FilterFilesOptions,
) {
const items = Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];

const filteredItems = items.reduce<T[]>((result: T[], item) => {
const useItem = shouldProcessItem(item, vars, options);

if (useItem) {
result.push(item);
}

return result;
}, []);

return filteredItems[0];
}

function shouldProcessItem<T extends Filter>(
item: T,
vars: Record<string, string>,
Expand Down

0 comments on commit 45e118f

Please sign in to comment.