Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ transform(readFileSync('README.md', {
// closingComment: '<!-- tocstop --> ',
// checkOpeningComments: ['<!-- toc '],
// checkClosingComments: ['<!-- tocstop '],
// isCustomMode: false,
// customTemplate: '<p align="center">${ITEMS}</p>',
// itemTemplate: '<a href="${LINK}">${TEXT}</a>',
// separator: '<span>|</span>',
}));
```

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {transform} from '../src';
describe('transform', () => {
it('run in html mode', () => {
const content = readFileSync(resolve(__dirname, 'fixtures/readme-with-html.md'), 'utf8');
const headers = transform(content, {mode: 'github.com', isHtml: true});
const headers = transform(content, {mode: 'github.com', isCustomMode: true});

expect(headers.toc.split('\n')).toEqual(
['**Table of Contents** *generated with [DocToc](https://github.com/technote-space/doctoc)*',
Expand All @@ -26,8 +26,8 @@ describe('transform', () => {
const content = readFileSync(resolve(__dirname, 'fixtures/readme-with-html.md'), 'utf8');
const headers = transform(content, {
mode: 'github.com',
isHtml: true,
htmlTemplate: '<ul>${ITEMS}</ul>',
isCustomMode: true,
customTemplate: '<ul>${ITEMS}</ul>',
itemTemplate: '<li><a href="${LINK}" target="_blank">${TEXT}</a></li>',
separator: '',
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@technote-space/doctoc",
"version": "2.2.1",
"version": "2.2.2",
"description": "Generates TOC for markdown files of local git repo.",
"keywords": [
"github",
Expand Down
8 changes: 4 additions & 4 deletions src/lib/transform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ const getHeaderItem = (header: HeaderWithAnchor, indentation: string, lowestRank
return `${indentation.repeat(header.rank - lowestRank)}${entryPrefix} ${header.anchor}`;
};

const getHtmlHeaderContents = (headers: Array<HeaderWithAnchor>, lowestRank: number, htmlTemplate: string | undefined, itemTemplate: string | undefined, separator: string | undefined): string => replaceVariables(htmlTemplate ?? DEFAULT_HTML_TEMPLATE, [{
const getHtmlHeaderContents = (headers: Array<HeaderWithAnchor>, lowestRank: number, customTemplate: string | undefined, itemTemplate: string | undefined, separator: string | undefined): string => replaceVariables(customTemplate ?? DEFAULT_HTML_TEMPLATE, [{
key: 'ITEMS',
replace: `\n${headers.filter(header => header.rank === lowestRank).map(header => getHeaderItemHtml(header, itemTemplate)).join(`\n${separator ?? DEFAULT_SEPARATOR}\n`)}\n`,
}]);
Expand Down Expand Up @@ -154,8 +154,8 @@ export const transform = (
closingComment,
checkOpeningComments,
checkClosingComments,
isHtml,
htmlTemplate,
isCustomMode,
customTemplate,
itemTemplate,
separator,
}: TransformOptions = {},
Expand Down Expand Up @@ -197,7 +197,7 @@ export const transform = (
const toc =
inferredTitle +
titleSeparator +
(isHtml ? getHtmlHeaderContents(linkedHeaders, lowestRank, htmlTemplate, itemTemplate, separator) : getHeaderContents(linkedHeaders, indentation, lowestRank, _entryPrefix)) +
(isCustomMode ? getHtmlHeaderContents(linkedHeaders, lowestRank, customTemplate, itemTemplate, separator) : getHeaderContents(linkedHeaders, indentation, lowestRank, _entryPrefix)) +
'\n';
const wrappedToc = (openingComment ?? OPENING_COMMENT) + '\n' + wrapToc(toc, inferredTitle, isFolding) + '\n' + (closingComment ?? CLOSING_COMMENT);
if (currentToc === wrappedToc) {
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export type TransformOptions = Partial<{
closingComment: string;
checkOpeningComments: Array<string>;
checkClosingComments: Array<string>;
isHtml: boolean;
htmlTemplate: string;
isCustomMode: boolean;
customTemplate: string;
itemTemplate: string;
separator: string;
}>
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1482,9 +1482,9 @@ copy-descriptor@^0.1.0:
integrity sha1-Z29us8OZl8LuGsOpJP1hJHSPV40=

core-js@^3.6.1:
version "3.7.0"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.7.0.tgz#b0a761a02488577afbf97179e4681bf49568520f"
integrity sha512-NwS7fI5M5B85EwpWuIwJN4i/fbisQUwLwiSNUWeXlkAZ0sbBjLEvLvFLf1uzAUV66PcEPt4xCGCmOZSxVf3xzA==
version "3.8.0"
resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.8.0.tgz#0fc2d4941cadf80538b030648bb64d230b4da0ce"
integrity sha512-W2VYNB0nwQQE7tKS7HzXd7r2y/y2SVJl4ga6oH/dnaLFzM0o2lB2P3zCkWj5Wc/zyMYjtgd5Hmhk0ObkQFZOIA==

[email protected], core-util-is@~1.0.0:
version "1.0.2"
Expand Down