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 Cli #4

Merged
merged 4 commits into from
Nov 20, 2024
Merged
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
22 changes: 20 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Easily generate a markdown Table of Contents (TOC) for your GitHub repository.
Install the package using npm:

```bash
npm install repo-toc
npm install -g repo-toc
```

## Usage
Expand Down Expand Up @@ -42,6 +42,24 @@ The generated Table of Contents:
<!---TOC-END--->
```

### CLI
```bash
repo-toc [options]

Options:
--version Show version number [boolean]
-d, --dir Directory path to generate TOC for
[string] [default: process.cwd()]
-e, --ext File extensions to include (comma-separated)
[string] [default: ".md"]
-o, --output File path to save the TOC [string] [default: "./README.md"]
-h, --help Show help [boolean]
```

```
repo-toc
```

### Code Example

To generate the TOC:
Expand All @@ -54,7 +72,7 @@ fs.writeFileSync(filePath, "## Table of contents");
generateTableOfContent({ dirPath, filePath });

// Call the function to generate the TOC
const toc = generateTableOfContent();
generateTableOfContent();
// It will update the README.md file with the Table of Contents
```

Expand Down
38 changes: 38 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env node

const yargs = require("yargs");
const { generateTableOfContent } = require("./lib");

yargs
.scriptName("repo-toc")
.usage("$0 [options]")
.option("dir", {
alias: "d",
describe: "Directory path to generate TOC for",
type: "string",
demandOption: false,
default: process.cwd(),
})
.option("ext", {
alias: "e",
describe: "File extensions to include (comma-separated)",
type: "string",
demandOption: false,
default: ".md",
})
.option("output", {
alias: "o",
describe: "File path to save the TOC",
type: "string",
demandOption: false,
default: "./README.md",
})
.help()
.alias("help", "h").argv;

const options = yargs.argv;
const dirPath = options.dir;
const extensions = options.ext.split(",");
const filePath = options.output;

generateTableOfContent({ dirPath, extensions, filePath });
28 changes: 4 additions & 24 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,7 @@
const fs = require("fs");
const path = require("path");

function getDefaultFileTitle(filePath) {
const fileSegments = filePath.split("/");
const fileName = fileSegments[fileSegments.length - 1];
const ext = path.extname(fileName);
return fileName.split(ext)[0];
}

function getMarkdownTitle(filePath) {
const content = fs.readFileSync(filePath, "utf8");

const titleMatch = content.match(/^#\s+(.*)/m);

if (titleMatch) {
return titleMatch[1].trim();
}
return getDefaultFileTitle(filePath);
}

function getFileTitle(filePath) {
if ([".MD", ".md"].includes(path.extname(filePath))) {
return getMarkdownTitle(filePath);
}
return getDefaultFileTitle(filePath);
}
const { shouldSkipFolder, getFileTitle } = require("./utils");

function getTableOfContents({ dirPath = process.cwd(), extensions = [] }) {
try {
Expand All @@ -45,6 +22,9 @@ function getTableOfContents({ dirPath = process.cwd(), extensions = [] }) {
const stat = fs.statSync(fullPath);

if (stat.isDirectory()) {
if (shouldSkipFolder(file)) {
continue;
}
toc += `${" ".repeat(level * 2)}* **${file}**\n`;
readDirectory({
directory: fullPath,
Expand Down
36 changes: 36 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const fs = require("fs");
const path = require("path");

function shouldSkipFolder(folderName) {
return folderName.startsWith(".");
}

function getDefaultFileTitle(filePath) {
const fileSegments = filePath.split("/");
const fileName = fileSegments[fileSegments.length - 1];
const ext = path.extname(fileName);
return fileName.split(ext)[0];
}

function getMarkdownTitle(filePath) {
const content = fs.readFileSync(filePath, "utf8");

const titleMatch = content.match(/^#\s+(.*)/m);

if (titleMatch) {
return titleMatch[1].trim();
}
return getDefaultFileTitle(filePath);
}

function getFileTitle(filePath) {
if ([".MD", ".md"].includes(path.extname(filePath))) {
return getMarkdownTitle(filePath);
}
return getDefaultFileTitle(filePath);
}

module.exports = {
getFileTitle,
shouldSkipFolder,
};
27 changes: 7 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
{
"name": "repo-toc",
"version": "1.0.1",
"version": "1.0.2",
"description": "https://github.com/kmtusher97/repo-toc",
"main": "index.js",
"scripts": {
"test": "jest",
"test:watch": "jest --watch"
},
"bin": {
"repo-toc": "./cli.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/kmtusher97/repo-toc.git"
},
"keywords": [
"toc"
"toc",
"markdown-toc",
"table of contents"
],
"author": "kmtusher97",
"license": "ISC",
Expand All @@ -22,5 +27,8 @@
"homepage": "https://github.com/kmtusher97/repo-toc#readme",
"devDependencies": {
"jest": "^29.7.0"
},
"dependencies": {
"yargs": "^17.7.2"
}
}
Empty file added tests/mocks/.hidden/hiddne.md
Empty file.
Loading