diff --git a/README.md b/README.md index 7376211..12d564d 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ $ concat-md --toc --decrease-title-levels --file-name-as-title --dir-name-as-tit # Features -- Scans all markdown files in a directory, +- Scans all or specified markdown files in a directory, - Optionally ignores some files, - Concatenates all of them, - Adds table of contents, @@ -54,6 +54,8 @@ Usage Options --ignore - Glob patterns to exclude in 'dir'. + --include - Glob patterns to look for in 'dir'. + Default: "**/*.md" --toc - Adds table of the contents at the beginning of file. --decrease-title-levels - Whether to decrease levels of all titles in markdown file to set them below file and directory title levels. --start-title-level-at - Level to start file and directory levels. Default: 1 @@ -244,6 +246,7 @@ Concat function options. - [dirNameAsTitle](#optional-dirnameastitle) - [fileNameAsTitle](#optional-filenameastitle) - [ignore](#optional-ignore) +- [include](#optional-include) - [joinString](#optional-joinstring) - [startTitleLevelAt](#optional-starttitlelevelat) - [titleKey](#optional-titlekey) @@ -292,6 +295,16 @@ Glob patterns to exclude in `dir`. --- +#### `Optional` include + +• **include**? : _string | string[]_ + +_Defined in [index.ts:48](https://github.com/ozum/concat-md/blob/670ea75/src/index.ts#L49)_ + +Glob patterns to look for in `dir`. Default: "**/*.md" + +--- + #### `Optional` joinString • **joinString**? : _undefined | string_ diff --git a/src/bin/concat-md.ts b/src/bin/concat-md.ts index d3ce626..99864be 100644 --- a/src/bin/concat-md.ts +++ b/src/bin/concat-md.ts @@ -13,6 +13,7 @@ const lstat = fs.promises.lstat; interface Result extends meow.Result { flags: { ignore: string; + include: string; toc: boolean; tocLevel: string; decreaseTitleLevels: boolean; @@ -29,6 +30,7 @@ interface Result extends meow.Result { /** @ignore */ const FLAGS: meowOptions["flags"] = { ignore: { type: "string" }, + include: { type: "string" }, toc: { type: "boolean" }, tocLevel: { type: "string" }, decreaseTitleLevels: { type: "boolean" }, @@ -47,7 +49,8 @@ Usage Options --ignore - Glob patterns to exclude in 'dir'. - --toc - Adds table of the contents at the beginning of file. + --include - Glob patterns to look for in 'dir'. Default: "**/*.md" + --toc - Adds table of the contents at the beginning of file. Default: "**/*.md" --toc-level - Limit TOC entries to headings only up to the specified level. Default: 3 --decrease-title-levels - Whether to decrease levels of all titles in markdown file to set them below file and directory title levels. --start-title-level-at - Level to start file and directory levels. Default: 1 @@ -69,14 +72,25 @@ Examples `; /** - * Splites CSV string of paths from CLI into array of absolute paths. + * Splits CSV string from CLI into array of strings. * - * @param pathsCSV is comma split values of paths to split. + * @param valuesCSV is comma-split values to split. + * @returns array of string values. + * @ignore + */ +function splitStrings(valuesCSV: string): string[] { + return valuesCSV ? valuesCSV.split(/\s*,\s*/) : []; +} + +/** + * Splits CSV string of paths from CLI into array of absolute paths. + * + * @param pathsCSV is comma-split values of paths to split. * @returns array of absolute paths converted from relative to cwd(). * @ignore */ function splitPaths(pathsCSV: string): string[] { - return pathsCSV ? pathsCSV.split(/\s*,\s*/).map(f => resolve(f)) : []; + return splitStrings(pathsCSV).map(f => resolve(f)); } /** @ignore */ @@ -92,6 +106,7 @@ async function exec(): Promise { const flags = { ...cli.flags, ignore: splitPaths(cli.flags.ignore), + include: splitStrings(cli.flags.include), }; try { diff --git a/src/index.ts b/src/index.ts index dae82ca..0e7fbc9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,6 +48,10 @@ export interface ConcatOptions { * Glob patterns to exclude in `dir`. */ ignore?: string | string[]; + /** + * Glob patterns to look for in `dir`. Default: "** /*.md" + */ + include?: string | string[]; /** * Whether to decrease levels of all titles in markdown file to set them below file and directory title levels. */ @@ -89,7 +93,8 @@ function arrify(input: T | T[]): T[] { class MarkDownConcatenator { private dir: string; private toc: boolean; - private ignore: string | string[]; + private ignore: string[]; + private include: string[]; private decreaseTitleLevels: boolean; private startTitleLevelAt: number; private titleKey?: string; @@ -99,7 +104,6 @@ class MarkDownConcatenator { private visitedDirs: Set = new Set(); private fileTitleIndex: Map = new Map(); private tocLevel: number; - private files: File[] = []; public constructor( dir: string, @@ -107,6 +111,7 @@ class MarkDownConcatenator { toc = false, tocLevel = 3, ignore = [], + include = [], decreaseTitleLevels = false, startTitleLevelAt = 1, joinString = "\n", @@ -118,7 +123,8 @@ class MarkDownConcatenator { this.dir = dir; this.toc = toc; this.tocLevel = tocLevel; - this.ignore = ignore; + this.ignore = arrify(ignore); + this.include = include.length ? arrify(include) : ["**/*.md"]; this.decreaseTitleLevels = decreaseTitleLevels; this.startTitleLevelAt = startTitleLevelAt; this.joinString = joinString; @@ -132,12 +138,20 @@ class MarkDownConcatenator { } private async getFileNames(): Promise { - const paths = await globby([`**/*.md`], { cwd: this.dir, ignore: arrify(this.ignore) }); + const paths = await globby(this.include, { + cwd: this.dir, + expandDirectories: ["**/*.md"], + ignore: this.ignore, + }); return paths.map(path => join(this.dir, path)); } private getFileNamesSync(): string[] { - const paths = globby.sync([`**/*.md`], { cwd: this.dir, ignore: arrify(this.ignore) }); + const paths = globby.sync(this.include, { + cwd: this.dir, + expandDirectories: ["**/*.md"], + ignore: this.ignore, + }); return paths.map(path => join(this.dir, path)); } diff --git a/test/index.test.ts b/test/index.test.ts index 01a22e8..d5e87f2 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -13,12 +13,20 @@ describe("concat", () => { expect(result).toBe(expected); }); - it("should syncronously concat files as is.", async () => { + it("should synchronously concat files as is.", async () => { const result = concatMdSync(join(__dirname, "test-helper/main")); const expected = await getExpected("main-as-is.txt"); expect(result).toBe(expected); }); + it("should concat specified files only.", async () => { + const result = await concat(join(__dirname, "test-helper/main"), { + include: "dir-a/**/*.md", + }); + const expected = await getExpected("main-specified-only.txt"); + expect(result).toBe(expected); + }); + it("should concat files using meta key as title.", async () => { const result = await concat(join(__dirname, "test-helper/main"), { titleKey: "title", diff --git a/test/test-helper/expected/main-specified-only.txt b/test/test-helper/expected/main-specified-only.txt new file mode 100644 index 0000000..4b5b43e --- /dev/null +++ b/test/test-helper/expected/main-specified-only.txt @@ -0,0 +1,4 @@ + + + +# Doc A