Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
DaanV2 committed Oct 9, 2021
1 parent 7fb52cf commit 4c55a25
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 55 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[typescript]": {
"editor.formatOnSave": true,
"editor.insertSpaces": true
}
}
110 changes: 55 additions & 55 deletions source/traverse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const Template = `# {$HEADER$}
{$DOCUMENTS$}`;

const WriteOptions: fs.WriteFileOptions = {
encoding: 'utf8'
encoding: 'utf8'
}


Expand All @@ -20,55 +20,55 @@ const WriteOptions: fs.WriteFileOptions = {
* @returns True if the page was made.
*/
export function CreateFolder(folder: string): boolean {
if (folder.includes('.git'))
return false;

let SubFolders: string[] = [];
let Documents: string[] = [];

//Get childern of folder
let childern = fs.readdirSync(folder);

//If childern is not undefined and has content then process it
if (childern && childern.length > 0) {
//Loop over
for (let I = 0; I < childern.length; I++) {
//grab item
const child = childern[I];
let subfolder = path.join(folder, child);

//if child is an directory or not
if (fs.statSync(subfolder).isDirectory()) {
//Create index page, if succesfull we create a reference

if (CreateFolder(subfolder)) {
SubFolders.push(`- [${child}](./${encodeURI(child)}/index.md)`);
}
}
else {
//If the child is a .md page create a reference
if (child.endsWith('.md') && child != 'index.md') {
Documents.push(`- [${child.substring(0, child.length - 3)}](${child})`);
}
}
}
}

//If there are any reference made we create the index page and return succes
if (SubFolders.length > 0 || Documents.length > 0) {
let filepath = path.join(folder, 'index.md');
let Name = GetFolderName(folder);
console.log('writing: ' + filepath);

let Content = Template.replace(/\{\$HEADER\$\}/gi, Name);
Content = Content.replace(/\{\$CATEGORIES\$\}/gi, SubFolders.join('\r\n'));
Content = Content.replace(/\{\$DOCUMENTS\$\}/gi, Documents.join('\r\n'));

fs.writeFileSync(filepath, Content, WriteOptions);
return true;
}

return false;
if (folder.includes('.git'))
return false;

let SubFolders: string[] = [];
let Documents: string[] = [];

//Get childern of folder
let childern = fs.readdirSync(folder);

//If childern is not undefined and has content then process it
if (childern && childern.length > 0) {
//Loop over
for (let I = 0; I < childern.length; I++) {
//grab item
const child = childern[I];
let subfolder = path.join(folder, child);

//if child is an directory or not
if (fs.statSync(subfolder).isDirectory()) {
//Create index page, if succesfull we create a reference

if (CreateFolder(subfolder)) {
SubFolders.push(`- [${child}](./${encodeURI(child)}/index.md)`);
}
}
else {
//If the child is a .md page create a reference
if (child.endsWith('.md') && child != 'index.md') {
Documents.push(`- [${child.substring(0, child.length - 3)}](${encodeURI(child)})`);
}
}
}
}

//If there are any reference made we create the index page and return succes
if (SubFolders.length > 0 || Documents.length > 0) {
let filepath = path.join(folder, 'index.md');
let Name = GetFolderName(folder);
console.log('writing: ' + filepath);

let Content = Template.replace(/\{\$HEADER\$\}/gi, Name);
Content = Content.replace(/\{\$CATEGORIES\$\}/gi, SubFolders.join('\r\n'));
Content = Content.replace(/\{\$DOCUMENTS\$\}/gi, Documents.join('\r\n'));

fs.writeFileSync(filepath, Content, WriteOptions);
return true;
}

return false;
}


Expand All @@ -78,11 +78,11 @@ export function CreateFolder(folder: string): boolean {
* @returns The name of the folder
*/
function GetFolderName(folderpath: string): string {
let LastIndex = folderpath.lastIndexOf('/');
let LastIndex = folderpath.lastIndexOf('/');

if (LastIndex >= 0) {
return folderpath.substring(LastIndex + 1, folderpath.length);
}
if (LastIndex >= 0) {
return folderpath.substring(LastIndex + 1, folderpath.length);
}

return folderpath;
return folderpath;
}

0 comments on commit 4c55a25

Please sign in to comment.