Skip to content

Commit f4bb8e7

Browse files
Merge pull request #5 from technote-space/release/next-v2.0.0
release: v2.1.0
2 parents c67a5e0 + 33dad20 commit f4bb8e7

File tree

6 files changed

+25
-17
lines changed

6 files changed

+25
-17
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ transform(readFileSync('README.md', {
4545
// updateOnly: true,
4646
// openingComment: '<!-- toc -->',
4747
// closingComment: '<!-- tocstop --> ',
48-
// checkOpeningComment: '<!-- toc ',
49-
// checkClosingComment: '<!-- tocstop ',
48+
// checkOpeningComments: ['<!-- toc '],
49+
// checkClosingComments: ['<!-- tocstop '],
5050
}));
5151
```
5252

__tests__/transform-weird-headers.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ describe('transform', () => {
3333
it('change check toc comment', () => {
3434
const content = readFileSync(resolve(__dirname, 'fixtures/readme-with-weird-headers.md'), 'utf8');
3535
const headers = transform(content, {
36-
checkOpeningComment: '<!-- toc ',
37-
checkClosingComment: '<!-- tocstop ',
36+
checkOpeningComments: ['<!-- toc '],
37+
checkClosingComments: ['<!-- tocstop '],
3838
openingComment: '<!-- toc -->',
3939
closingComment: '<!-- tocstop -->',
4040
isNotitle: true,

__tests__/transform.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -597,24 +597,24 @@ describe('matchesStart', () => {
597597
it('should return true', () => {
598598
expect(matchesStart()('<!-- START doctoc -->')).toBe(true);
599599
expect(matchesStart()('<!-- START doctoc generated TOC please keep comment here to allow auto update -->')).toBe(true);
600-
expect(matchesStart('<!-- test ')('<!-- test abc -->')).toBe(true);
600+
expect(matchesStart(['<!-- test '])('<!-- test abc -->')).toBe(true);
601601
});
602602

603603
it('should return false', () => {
604604
expect(matchesStart()('<!-- doctoc -->')).toBe(false);
605-
expect(matchesStart('<!-- test ')('<!-- START doctoc -->')).toBe(false);
605+
expect(matchesStart(['<!-- test '])('<!-- START doctoc -->')).toBe(false);
606606
});
607607
});
608608

609609
describe('matchesEnd', () => {
610610
it('should return true', () => {
611611
expect(matchesEnd()('<!-- END doctoc -->')).toBe(true);
612612
expect(matchesEnd()('<!-- END doctoc generated TOC please keep comment here to allow auto update -->')).toBe(true);
613-
expect(matchesEnd('<!-- test ')('<!-- test abc -->')).toBe(true);
613+
expect(matchesEnd(['<!-- test '])('<!-- test abc -->')).toBe(true);
614614
});
615615

616616
it('should return false', () => {
617617
expect(matchesEnd()('<!-- doctoc -->')).toBe(false);
618-
expect(matchesEnd('<!-- test ')('<!-- END doctoc -->')).toBe(false);
618+
expect(matchesEnd(['<!-- test '])('<!-- END doctoc -->')).toBe(false);
619619
});
620620
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@technote-space/doctoc",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Generates TOC for markdown files of local git repo.",
55
"keywords": [
66
"github",

src/lib/transform.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ import {
1212
} from '..';
1313
import {TransformOptions, Header, HeaderWithRepetition, HeaderWithAnchor, SectionInfo, TransformResult} from '../types';
1414

15-
export const matchesStart = (openingComment?: string) => (line: string): boolean => (new RegExp(openingComment ? openingComment : CHECK_OPENING_COMMENT)).test(line);
16-
export const matchesEnd = (closingComment?: string) => (line: string): boolean => (new RegExp(closingComment ? closingComment : CHECK_CLOSING_COMMENT)).test(line);
15+
const getTargetComments = (checkComments: Array<string>, defaultComments: string): Array<string> => {
16+
if (checkComments.length) {
17+
return checkComments;
18+
}
19+
20+
return [defaultComments];
21+
};
22+
23+
export const matchesStart = (checkOpeningComments?: Array<string>) => (line: string): boolean => getTargetComments(checkOpeningComments ?? [], CHECK_OPENING_COMMENT).some(comment => new RegExp(comment).test(line));
24+
export const matchesEnd = (checkClosingComments?: Array<string>) => (line: string): boolean => getTargetComments(checkClosingComments ?? [], CHECK_CLOSING_COMMENT).some(comment => new RegExp(comment).test(line));
1725
const addAnchor = (mode: string | undefined, moduleName: string | undefined, header: HeaderWithRepetition): HeaderWithAnchor => {
1826
return {
1927
...header,
@@ -120,8 +128,8 @@ export const transform = (
120128
updateOnly,
121129
openingComment,
122130
closingComment,
123-
checkOpeningComment,
124-
checkClosingComment,
131+
checkOpeningComments,
132+
checkClosingComments,
125133
}: TransformOptions = {},
126134
): TransformResult => {
127135
mode = mode || 'github.com';
@@ -131,7 +139,7 @@ export const transform = (
131139
// eslint-disable-next-line no-magic-numbers
132140
const maxHeaderLevelHtml = maxHeaderLevel || 4;
133141
const lines = content.split('\n');
134-
const info: SectionInfo = updateSection.parse(lines, matchesStart(checkOpeningComment), matchesEnd(checkClosingComment));
142+
const info: SectionInfo = updateSection.parse(lines, matchesStart(checkOpeningComments), matchesEnd(checkClosingComments));
135143

136144
if (!info.hasStart && updateOnly) {
137145
return {
@@ -185,7 +193,7 @@ export const transform = (
185193

186194
return {
187195
transformed: true,
188-
data: updateSection(lines.join('\n'), wrappedToc, matchesStart(checkOpeningComment), matchesEnd(checkClosingComment), true),
196+
data: updateSection(lines.join('\n'), wrappedToc, matchesStart(checkOpeningComments), matchesEnd(checkClosingComments), true),
189197
toc,
190198
wrappedToc,
191199
reason: '',

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export type TransformOptions = Partial<{
1212
updateOnly: boolean;
1313
openingComment: string;
1414
closingComment: string;
15-
checkOpeningComment: string;
16-
checkClosingComment: string;
15+
checkOpeningComments: Array<string>;
16+
checkClosingComments: Array<string>;
1717
}>
1818

1919
export type FileInfo = {

0 commit comments

Comments
 (0)