Skip to content

Commit

Permalink
refactor: update extension options
Browse files Browse the repository at this point in the history
BREAKING CHANGE: merged 2 render chapter functions to 1
  • Loading branch information
huaichaow committed Feb 8, 2023
1 parent a36ef88 commit a935438
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 20 deletions.
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { numberingHeadingFactory } from './numberingHeadingFactory';
export default function markedTableOfContentsExtension(
options?: MarkedTableOfContentsExtensionOptions,
) {
const { className, renderChapterNumberHeading, renderChapterNumberTOC } = options || {};
const { className, renderChapterNumber } = options || {};

let headings: Array<{ text: string; depth: number }> | null = null;
let fixHeadingDepth: ((heading: Heading) => void) | null = null;
Expand All @@ -34,10 +34,7 @@ export default function markedTableOfContentsExtension(
fixHeadingDepth = fixHeadingDepthFactory();
}
if (!numberingHeading) {
numberingHeading = numberingHeadingFactory(
renderChapterNumberTOC,
renderChapterNumberHeading,
);
numberingHeading = numberingHeadingFactory(renderChapterNumber);
}
fixHeadingDepth(token);
numberingHeading(token);
Expand Down
3 changes: 1 addition & 2 deletions src/numberingHeadingFactory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ describe('numberingHeadingFactory', () => {
const headings = createHeadings(receivedDepths);

numberingHeading = numberingHeadingFactory(
(numbers: Array<number>) => numbers.join("-"),
(numbers: Array<number>) => numbers.join("|"),
(numbers: Array<number>, kind) => numbers.join(kind === 'toc' ? '-' : '|'),
);

headings.forEach((heading) => numberingHeading(heading));
Expand Down
13 changes: 5 additions & 8 deletions src/numberingHeadingFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import { Heading, HeadingWithChapterNumber, RenderChapterNumberFn } from './type
* it makes no sense to numbering these headings.
*/

export function numberingHeadingFactory(
renderChapterNumberTOC?: RenderChapterNumberFn,
renderChapterNumberHeading?: RenderChapterNumberFn,
) {
export function numberingHeadingFactory(renderChapterNumber?: RenderChapterNumberFn) {
const chapterNumbers: Array<number> = [];
let prevDepth = 0;

Expand Down Expand Up @@ -46,13 +43,13 @@ export function numberingHeadingFactory(

// append chapter number to token
(heading as unknown as HeadingWithChapterNumber).chapterNumberTOC =
renderChapterNumberTOC
? renderChapterNumberTOC([...chapterNumbers])
renderChapterNumber
? renderChapterNumber([...chapterNumbers], 'toc')
: getChapterNumber();

(heading as unknown as HeadingWithChapterNumber).chapterNumberHeading =
renderChapterNumberHeading
? renderChapterNumberHeading([...chapterNumbers])
renderChapterNumber
? renderChapterNumber([...chapterNumbers], 'heading')
: getChapterNumber();
};
}
3 changes: 1 addition & 2 deletions src/test/toc-with-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import markedToc from '..';

marked.use(markedToc({
className: 'toc',
renderChapterNumberTOC: (numbers) => numbers.join("--"),
renderChapterNumberHeading: (numbers) => numbers.join("++"),
renderChapterNumber: (numbers, kind) => numbers.join(kind === 'toc' ? '--' : '++'),
}));

function removeLeadingSpaces(html: string) {
Expand Down
5 changes: 2 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ export type HeadingWithChapterNumber = Heading & {

export type Headings = Array<Heading>;

export type RenderChapterNumberFn = (numbers: Array<number>) => string;
export type RenderChapterNumberFn = (numbers: Array<number>, kind: 'toc' | 'heading') => string;

export type MarkedTableOfContentsExtensionOptions = {
className?: string;
renderChapterNumberTOC?: RenderChapterNumberFn;
renderChapterNumberHeading?: RenderChapterNumberFn;
renderChapterNumber?: RenderChapterNumberFn;
};

0 comments on commit a935438

Please sign in to comment.