Skip to content

Commit 832c08c

Browse files
committed
Fixed issue with flags
1 parent d4ffab5 commit 832c08c

File tree

5 files changed

+56
-17
lines changed

5 files changed

+56
-17
lines changed

bin/run

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ const MarkdownIndexGenerator = require('../lib/index.js');
77
program
88
.version(version)
99
.arguments('<file>', 'The file to parse')
10-
.option('-o', '--output <outputFile>', 'Output will be saved in the defined file')
11-
.option('-d', '--depth <depth>', 'Depth of the headings to parse, 4 means \'till h4\'', 4)
12-
.option('-t', '--title <title>', 'Title to add on top of the index, by default is \'## Index\'')
13-
.option('-r', '--replace', 'Add the index to the source file', false)
10+
.option('-o, --output <outputFile>', 'Output will be saved in the defined file')
11+
.option('-d, --depth <depth>', 'Depth of the headings to parse, 4 means \'till h4\'', 4)
12+
.option('-t, --title <title>', 'Title to add on top of the index, by default is \'## Index\'')
13+
.option('-r, --replace <replace>', 'Add the index to the source file', false)
1414
.action(MarkdownIndexGenerator);
1515

1616
program.parse();

src/constants.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
1+
import { Flags } from './interfaces/types';
2+
13
export const INDEX_TAG = 'index';
24
export const VALID_FORMATS = ['md', 'mdx'];
5+
export const DEFAULT_VALUES: Flags = {
6+
replace: false,
7+
depth: 4,
8+
}

src/index.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
import MarkDownParser from './classes/markdown'
1+
import MarkDownParser from './classes/markdown';
2+
import { Flags } from './interfaces/types';
3+
import { DEFAULT_VALUES } from './constants';
24

3-
interface Flags {
4-
depth?: number;
5-
title?: string;
6-
output?: string;
7-
replace?: string;
8-
}
5+
const MarkdownIndexGenerator = async(file: string, flags?: Flags) => {
6+
if (!flags) {
7+
flags = {...DEFAULT_VALUES};
8+
}
99

10-
const MarkdownIndexGenerator = async(file: string, flags: Flags) => {
1110
if (!file) {
1211
throw 'Missing file to parse';
1312
}
1413

1514
const parser = new MarkDownParser(file);
1615

17-
if (flags.depth) {
16+
if (flags?.depth) {
1817
parser.setDepth(flags.depth);
1918
}
2019

21-
if (flags.title) {
20+
if (flags?.title) {
2221
parser.setTitle(flags.title);
2322
}
2423

2524
await parser.parse();
2625

27-
if (flags.output) {
26+
if (flags?.output) {
2827
await parser.toFile(flags.output);
2928
console.log(`File ${flags.output} saved!`);
30-
} else if (flags.replace) {
29+
} else if (flags?.replace) {
3130
await parser.replaceOriginal();
3231
console.log(`File ${file} updated!`);
3332
} else {

src/interfaces/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1-
export type HeadingsRange = 2|3|4|5
1+
export type HeadingsRange = 2|3|4|5;
2+
3+
export interface Flags {
4+
depth: number;
5+
replace: boolean;
6+
title?: string;
7+
output?: string;
8+
}

test/index.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import MarkdownIndexGenerator from '../src/index'
2+
3+
describe('MarkdownIndexGenerator', () => {
4+
beforeEach(() => {
5+
console.log = jest.fn();
6+
});
7+
8+
afterEach(() => {
9+
jest.clearAllMocks();
10+
});
11+
12+
it('should show the generated menu', async () => {
13+
await MarkdownIndexGenerator('./test/__mocks__/test.md');
14+
expect(console.log).toHaveBeenCalledWith('--- Begin MarkDown ---');
15+
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('[Heading 2](#heading-2)'));
16+
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('- [Sub sub heading 4](#sub-sub-heading-4)'));
17+
expect(console.log).toHaveBeenCalledWith('--- End Markdown ---');
18+
});
19+
20+
it('should show the generated menu, but only at depth 2', async () => {
21+
await MarkdownIndexGenerator('./test/__mocks__/test.md', { depth: 2, replace: false});
22+
expect(console.log).toHaveBeenCalledWith('--- Begin MarkDown ---');
23+
expect(console.log).toHaveBeenCalledWith(expect.stringContaining('[Heading 2](#heading-2)'));
24+
expect(console.log).toHaveBeenCalledWith(expect.not.stringContaining('- [Sub sub heading 4](#sub-sub-heading-4)'));
25+
expect(console.log).toHaveBeenCalledWith('--- End Markdown ---');
26+
});
27+
});

0 commit comments

Comments
 (0)