Skip to content

Commit

Permalink
Options: single
Browse files Browse the repository at this point in the history
  • Loading branch information
ifuture-pro committed Jan 21, 2020
1 parent 7f1b8be commit 2ca4f47
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 19 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ listify ./
- `--blog` parse YAML `frontmatter` block in the Markdown file.
Front matter options `title`,`publish`
- `--package` Parent directory as the first header
- `--single` Directories in single. Default:false
- `--merge` Merge directories. Default:true
- `--exclude_dir` Exclude dir.

## Github actions
Expand Down
31 changes: 31 additions & 0 deletions demo/first.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,34 @@
<!-- start listify -->

Table of Contents
-----------
> *generated with [listify](https://github.com/ifuture-pro/listify)*
# [first](first.md#first)

- [Table of Contents](first.md#table-of-contents)
- [first](first.md#first)
- [Typographic replacements](first.md#typographic-replacements)
- [Emphasis](first.md#emphasis)
- [Blockquotes](first.md#blockquotes)
- [Lists](first.md#lists)
- [Code](first.md#code)
- [Tables](first.md#tables)
- [Links](first.md#links)
- [Images](first.md#images)
- [Plugins](first.md#plugins)
- [Emojies](first.md#emojies)
- [Subscript / Superscript](first.md#subscript--superscript)
- [\<ins>](first.md#%5Cins)
- [\<mark>](first.md#%5Cmark)
- [Footnotes](first.md#footnotes)
- [Definition lists](first.md#definition-lists)
- [Abbreviations](first.md#abbreviations)
- [Custom containers](first.md#custom-containers)
<!-- end listify -->



## Typographic replacements

Enable typographer option to see result.
Expand Down
34 changes: 34 additions & 0 deletions demo/second.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
<!-- start listify -->

Table of Contents
-----------
> *generated with [listify](https://github.com/ifuture-pro/listify)*
# [second](second.md#second)

- [Table of Contents](second.md#table-of-contents)
- [second](second.md#second)
- [Welcome to StackEdit!](second.md#welcome-to-stackedit)
- [Files](second.md#files)
- [Create files and folders](second.md#create-files-and-folders)
- [Switch to another file](second.md#switch-to-another-file)
- [Rename a file](second.md#rename-a-file)
- [Delete a file](second.md#delete-a-file)
- [Export a file](second.md#export-a-file)
- [Synchronization](second.md#synchronization)
- [Open a file](second.md#open-a-file)
- [Save a file](second.md#save-a-file)
- [Synchronize a file](second.md#synchronize-a-file)
- [Manage file synchronization](second.md#manage-file-synchronization)
- [Publication](second.md#publication)
- [Publish a File](second.md#publish-a-file)
- [Update a publication](second.md#update-a-publication)
- [Manage file publication](second.md#manage-file-publication)
- [Markdown extensions](second.md#markdown-extensions)
- [SmartyPants](second.md#smartypants)
- [KaTeX](second.md#katex)
- [UML diagrams](second.md#uml-diagrams)
<!-- end listify -->



# Welcome to StackEdit!

Hi! I'm your first Markdown file in **StackEdit**. If you want to learn about StackEdit, you can read me. If you want to play with Markdown, you can edit me. Once you have finished with me, you can create new files by opening the **file explorer** on the left corner of the navigation bar.
Expand Down
18 changes: 18 additions & 0 deletions demo/topicA/topicAA.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<!-- start listify -->

Table of Contents
-----------
> *generated with [listify](https://github.com/ifuture-pro/listify)*
# topicA

## [topicAA](topicA/topicAA.md#topicaa)

- [Table of Contents](topicA/topicAA.md#table-of-contents)
- [topicA](topicA/topicAA.md#topica)
- [topicAA](topicA/topicAA.md#topicaa)
- [topicA . A](topicA/topicAA.md#topica--a)
<!-- end listify -->



## topicA . A

contents
15 changes: 15 additions & 0 deletions demo/topicA/topicAB.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
<!-- start listify -->

Table of Contents
-----------
> *generated with [listify](https://github.com/ifuture-pro/listify)*
## [topicAB](topicA/topicAB.md#topicab)

- [Table of Contents](topicA/topicAB.md#table-of-contents)
- [topicAB](topicA/topicAB.md#topicab)
- [topicA . B](topicA/topicAB.md#topica--b)
<!-- end listify -->



## topicA . B

contents
18 changes: 18 additions & 0 deletions demo/topicA/topicAC/topicACA.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
<!-- start listify -->

Table of Contents
-----------
> *generated with [listify](https://github.com/ifuture-pro/listify)*
## topicAC

### [topicACA](topicA/topicAC/topicACA.md#topicaca)

- [Table of Contents](topicA/topicAC/topicACA.md#table-of-contents)
- [topicAC](topicA/topicAC/topicACA.md#topicac)
- [topicACA](topicA/topicAC/topicACA.md#topicaca)
- [topicA . C . A](topicA/topicAC/topicACA.md#topica--c--a)
<!-- end listify -->



## topicA . C . A

contents
17 changes: 17 additions & 0 deletions demo/topicA/topicAC/topicACB/topicACBA.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!-- start listify -->

Table of Contents
-----------
> *generated with [listify](https://github.com/ifuture-pro/listify)*
### topicACB

#### [topicACBA](topicA/topicAC/topicACB/topicACBA.md#topicacba)

- [Table of Contents](topicA/topicAC/topicACB/topicACBA.md#table-of-contents)
- [topicACB](topicA/topicAC/topicACB/topicACBA.md#topicacb)
- [topicACBA](topicA/topicAC/topicACB/topicACBA.md#topicacba)
<!-- end listify -->



30 changes: 17 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,27 @@ function help() {
console.log('Usage: listify <path> [options]\n' +
'options:\n'+
'- `--out` Result out file. Default: work dir README.md\n' +
'- `--maxlevel` Header level. Default: 4\n' +
'- `--title` The Title of list\n' +
'- `--containroot` Contain root path.When linked in local,change it `true`.Default: `false`\n' +
'- `--suffix` contain file suffix in out file.Default: `false`\n' +
'- `--exclude` Exclude files. \n' +
' e.g. \n' +
' Ignore multi file `--exclude a.md --exclude b.md ` \n' +
' Support regexp. Ignore start with `_` `--exclude=\'^_\\S*\'`\n' +
'- `--blog` parse YAML `frontmatter` block in the Markdown file. \n' +
' Front matter options `title`,`publish`\n' +
'- `--package` Parent directory as the first header\n' +
'- `--exclude_dir` Exclude dir.');
'- `--maxlevel` Header level. Default: 4\n' +
'- `--title` The Title of list\n' +
'- `--containroot` Contain root path.When linked in local,change it `true`.Default: `false`\n' +
'- `--suffix` contain file suffix in out file.Default: `false`\n' +
'- `--exclude` Exclude files. \n' +
' e.g. \n' +
' Ignore multi file `--exclude a.md --exclude b.md ` \n' +
' Support regexp. Ignore start with `_` `--exclude=\'^_\\S*\'`\n' +
'- `--blog` parse YAML `frontmatter` block in the Markdown file. \n' +
' Front matter options `title`,`publish`\n' +
'- `--package` Parent directory as the first header\n' +
'- `--single` Directories in single. Default:false\n' +
'- `--merge` Merge directories. Default:true\n' +
'- `--exclude_dir` Exclude dir.');
process.exit(0);
}



const args = minimist(process.argv.slice(2),
{ boolean: ['h','help','blog','containroot','suffix', 'package'],
{ boolean: ['h','help','blog','containroot','suffix', 'package','merge'],
string: ['title', 'maxlevel', 'out', 'exclude', 'exclude_dir']
});

Expand All @@ -92,6 +94,8 @@ let config = {
suffix: args.suffix || false,
blog: args.blog || false,
package: args.package || false,
merge: args.merge || true,
single: args.single || false,
prefix: '-',
markStart: '<!-- start listify -->',
markEnd: '<!-- end listify -->'
Expand Down
27 changes: 24 additions & 3 deletions lib/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ function merge(markdownFiles) {
let all = [];
let lastDir = [];
for (let i = 0; i < markdownFiles.length; i++) {
let currentContent = [];
let parsed = generateList(markdownFiles[i]);
if (parsed) {
if (config.package) {
Expand All @@ -127,21 +128,41 @@ function merge(markdownFiles) {
let filted = currentDir.filter(c => lastDir.indexOf(c) < 0);
lastDir = currentDir;
for (let i = 0; i < filted.length; i++) {
all.push(getHeadIndent(i+currentDir.length-filted.length) + filted[i])
currentContent.push(getHeadIndent(i+currentDir.length-filted.length) + filted[i])
}
}

}
all.push(getHeadIndent(lastDir.length) + parsed.title);
all.push(parsed.headers.map(p => {
currentContent.push(getHeadIndent(lastDir.length) + parsed.title);
currentContent.push(parsed.headers.map(p => {
return getIndent(p.depth) + config.prefix + ' ' + p.anchor
}).join("\n"));
if (config.merge) {
all.concat(currentContent);
}
if (config.single) {
append(markdownFiles[i],currentContent.join("\n\n"));
}
}
}
return all.join('\n\n');
}

function append(file,content) {
content = config.markStart + '\n' + config.title + '\n' + content + '\n' + config.markEnd + '\n';
let old = fs.readFileSync(file, 'utf8');
if (new RegExp(config.markStart, 'gi').test(old) && new RegExp(config.markEnd, 'gi').test(old)) {
content = old.replace(new RegExp(config.markStart + '[\\s\\S]*' + config.markEnd, 'gi'), content);
} else {
content = content + '\n\n' + old;
}
fs.writeFileSync(file, content, 'utf-8')
}

function save(result) {
if (result.trim().length < 5) {
return;
}
result = config.markStart + '\n' + config.title + '\n' + result + '\n' + config.markEnd + '\n';

if (fs.existsSync(config.outFile)) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "@ifuture/listify",
"version": "0.1.7",
"version": "0.1.8",
"description": "Auto generate table of contents at full-site",
"main": "index.js",
"scripts": {
"test": "node index.js ./demo/ --package"
"test": "node index.js ./demo/ --package --merge=false --single"
},
"bin": {
"listify": "index.js"
Expand Down

0 comments on commit 2ca4f47

Please sign in to comment.