Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "include" option to specify patterns to look for in the input directory #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -54,6 +54,8 @@ Usage

Options
--ignore <globs csv> - Glob patterns to exclude in 'dir'.
--include <globs csv> - 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 no> - Level to start file and directory levels. Default: 1
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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_
Expand Down
23 changes: 19 additions & 4 deletions src/bin/concat-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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" },
Expand All @@ -47,7 +49,8 @@ Usage

Options
--ignore <globs csv> - Glob patterns to exclude in 'dir'.
--toc - Adds table of the contents at the beginning of file.
--include <globs csv> - 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 no> - Level to start file and directory levels. Default: 1
Expand All @@ -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 */
Expand All @@ -92,6 +106,7 @@ async function exec(): Promise<void> {
const flags = {
...cli.flags,
ignore: splitPaths(cli.flags.ignore),
include: splitStrings(cli.flags.include),
};

try {
Expand Down
24 changes: 19 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -89,7 +93,8 @@ function arrify<T>(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;
Expand All @@ -99,14 +104,14 @@ class MarkDownConcatenator {
private visitedDirs: Set<string> = new Set();
private fileTitleIndex: Map<string, { title: string; level: number; md: string }> = new Map();
private tocLevel: number;
private files: File[] = [];

public constructor(
dir: string,
{
toc = false,
tocLevel = 3,
ignore = [],
include = [],
decreaseTitleLevels = false,
startTitleLevelAt = 1,
joinString = "\n",
Expand All @@ -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;
Expand All @@ -132,12 +138,20 @@ class MarkDownConcatenator {
}

private async getFileNames(): Promise<string[]> {
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));
}

Expand Down
10 changes: 9 additions & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 4 additions & 0 deletions test/test-helper/expected/main-specified-only.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

<a name="dir-aamd"></a>

# Doc A