-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: customize chapter number rendering
- Loading branch information
Showing
8 changed files
with
138 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* todo: marked does not support reset defaults, so need to create a separate | ||
* test module to test the extension with different options. | ||
*/ | ||
|
||
import { marked } from 'marked'; | ||
import markedToc from '..'; | ||
|
||
marked.use(markedToc({ | ||
renderChapterNumberTOC: (numbers) => numbers.join("--"), | ||
renderChapterNumberHeading: (numbers) => numbers.join("++"), | ||
})); | ||
|
||
function removeLeadingSpaces(html: string) { | ||
return html | ||
.replace(/\n +/g, '\n') | ||
.replace(/^\s+/, ''); | ||
} | ||
|
||
describe('marked-toc-extension with options', () => { | ||
test('should render custom chapter numbers', () => { | ||
const md = removeLeadingSpaces(` | ||
# a | ||
## b | ||
### c | ||
[TOC] | ||
`); | ||
|
||
const expectedHtml = removeLeadingSpaces(` | ||
<h1 id="a">1 a</h1> | ||
<h2 id="b">1++1 b</h2> | ||
<h3 id="c">1++1++1 c</h3> | ||
<ul><li>1 a</li><ul><li>1--1 b</li><ul><li>1--1--1 c</li></ul></ul></ul> | ||
`); | ||
|
||
expect(marked.parse(md)).toEqual(expectedHtml); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,18 @@ | ||
export type Heading = { | ||
text: string; | ||
depth: number; | ||
text: string; | ||
depth: number; | ||
} | ||
|
||
export type HeadingWithChapterNumber = Heading & { | ||
chapterNumber: string; // dot separated string, e.g., `1.2.3` | ||
chapterNumberTOC: string; | ||
chapterNumberHeading: string; | ||
} | ||
|
||
export type Headings = Array<Heading>; | ||
|
||
export type RenderChapterNumberFn = (numbers: Array<number>) => string; | ||
|
||
export type MarkedTableOfContentsExtensionOptions = { | ||
renderChapterNumberTOC?: RenderChapterNumberFn; | ||
renderChapterNumberHeading?: RenderChapterNumberFn; | ||
}; |