Skip to content

Commit 627dbf5

Browse files
feat: add skip feature (#93)
1 parent 5cfb3a7 commit 627dbf5

File tree

7 files changed

+48
-2
lines changed

7 files changed

+48
-2
lines changed

__tests__/file.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ describe('findMarkdownFiles', () => {
2626
'name': 'readme-not-updated.md',
2727
'path': resolve(__dirname, 'fixtures/readme-not-updated.md'),
2828
},
29+
{
30+
'name': 'readme-skipped.md',
31+
'path': resolve(__dirname, 'fixtures/readme-skipped.md'),
32+
},
2933
{
3034
'name': 'readme-update-only.md',
3135
'path': resolve(__dirname, 'fixtures/readme-update-only.md'),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!-- DOCTOC SKIP -->
2+
# Hello, world!
3+
4+
> You can make code blocks with three back ticks:
5+
>
6+
> ```
7+
8+
# Add this header
9+
10+
<h1>And also this one</h1>
11+
12+
> ```
13+
14+

__tests__/transform-not-transformed.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ describe('transform', () => {
2020
expect(headers.transformed).toBe(false);
2121
expect(headers.reason).toBe('not updated');
2222
});
23+
24+
it('skipped', () => {
25+
const content = readFileSync(resolve(__dirname, 'fixtures/readme-skipped.md'), 'utf8');
26+
const headers = transform(content);
27+
28+
expect(headers.transformed).toBe(false);
29+
expect(headers.reason).toBe('skipped');
30+
});
2331
});

__tests__/transform.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { describe, expect, it } from 'vitest';
33
import { transform } from '../src/index.js';
44
import { OPENING_COMMENT, CLOSING_COMMENT } from '../src/index.js';
5-
import { getLinesToToc, matchesStart, matchesEnd } from '../src/lib/transform.js';
5+
import { getLinesToToc, matchesStart, matchesEnd, matchesSkip } from '../src/lib/transform.js';
66

77
const check = (
88
name: string,
@@ -620,3 +620,15 @@ describe('matchesEnd', () => {
620620
expect(matchesEnd(['<!-- test '])('<!-- END doctoc -->')).toBe(false);
621621
});
622622
});
623+
624+
describe('matchesSkip', () => {
625+
it('should return true', () => {
626+
expect(matchesSkip()('<!-- DOCTOC SKIP -->')).toBe(true);
627+
expect(matchesSkip(['<!-- test '])('<!-- test abc -->')).toBe(true);
628+
});
629+
630+
it('should return false', () => {
631+
expect(matchesSkip()('<!-- doctoc -->')).toBe(false);
632+
expect(matchesSkip(['<!-- test '])('<!-- DOCTOC SKIP -->')).toBe(false);
633+
});
634+
});

src/constant.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export const OPENING_COMMENT = '<!-- START doctoc generated TOC please k
33
export const CLOSING_COMMENT = '<!-- END doctoc generated TOC please keep comment here to allow auto update -->';
44
export const CHECK_OPENING_COMMENT = '<!-- START doctoc ';
55
export const CHECK_CLOSING_COMMENT = '<!-- END doctoc ';
6+
export const CHECK_SKIP_COMMENT = '<!-- DOCTOC SKIP ';
67
export const DEFAULT_TITLE = '**Table of Contents** *generated with [DocToc](https://github.com/technote-space/doctoc)*';
78
export const MARKDOWN_EXTENSIONS = ['.md', '.markdown'];
89
export const IGNORED_DIRS = ['.', '..', '.git', 'node_modules'];

src/lib/transform.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
CLOSING_COMMENT,
99
CHECK_OPENING_COMMENT,
1010
CHECK_CLOSING_COMMENT,
11+
CHECK_SKIP_COMMENT,
1112
DEFAULT_TITLE,
1213
DEFAULT_CUSTOM_TEMPLATE,
1314
DEFAULT_ITEM_TEMPLATE,
@@ -27,6 +28,7 @@ const getTargetComments = (checkComments: Array<string>, defaultComments: string
2728

2829
export const matchesStart = (checkOpeningComments?: Array<string>) => (line: string): boolean => getTargetComments(checkOpeningComments ?? [], CHECK_OPENING_COMMENT).some(comment => new RegExp(comment).test(line));
2930
export const matchesEnd = (checkClosingComments?: Array<string>) => (line: string): boolean => getTargetComments(checkClosingComments ?? [], CHECK_CLOSING_COMMENT).some(comment => new RegExp(comment).test(line));
31+
export const matchesSkip = (checkSkipComments?: Array<string>) => (line: string): boolean => getTargetComments(checkSkipComments ?? [], CHECK_SKIP_COMMENT).some(comment => new RegExp(comment).test(line));
3032
const addAnchor = (mode: string, moduleName: string | undefined, header: HeaderWithRepetition): HeaderWithAnchor => {
3133
return {
3234
...header,
@@ -185,7 +187,11 @@ export const transform = (
185187
content: string,
186188
options: TransformOptions = {},
187189
): TransformResult => {
188-
const lines = content.split('\n');
190+
const lines = content.split('\n');
191+
if (lines.some(matchesSkip(options.checkSkipComments))) {
192+
return getResult({ transformed: false, reason: 'skipped' });
193+
}
194+
189195
const info: SectionInfo = updateSection.parse(lines, matchesStart(options.checkOpeningComments), matchesEnd(options.checkClosingComments));
190196

191197
const startSection = getStartSection(lines, info, matchesEnd(options.checkClosingComments));

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export type TransformOptions = Partial<{
1414
closingComment: string;
1515
checkOpeningComments: Array<string>;
1616
checkClosingComments: Array<string>;
17+
checkSkipComments: Array<string>;
1718
isCustomMode: boolean;
1819
customTemplate: string;
1920
itemTemplate: string;

0 commit comments

Comments
 (0)