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

Improvements suggestion which enables creating whole project of documentation #19

Open
ronshe opened this issue Aug 23, 2018 · 3 comments

Comments

@ronshe
Copy link

ronshe commented Aug 23, 2018

Changes:
Enable setting the output folder as second argument (first is the input folder).
Line 17: const [docsFolder, outputFolder, ...argsRest] = process.argv.slice(2);
Line 22: const output = (outputFolder) ? outputFolder : `_${folder}`;

When using current folder as source folder (Like: markdown-folder-to-html ./)' the output folder is _. and is located inside the current folder. It causes problem with the recursive copy (cp -R . ./).
The solution is to ignore the output directory. In my case it caused problem because of coping folder node_modules too. I do not want it in the project and it could not be renamed.
Solution line 52: sh.cp("-R", folder + `/!(node_modules|${output})`, output);

Source: (cli.ts)

#! /usr/bin/env node

import fs from "fs";
import path from "path";
import sh from "shelljs";

import groupByPath from "./lib/group-by-path";
import sortByPreferences from "./lib/sort-by-preferences";
import mdUrl from "./lib/markdown-url-to-html";
import md2html from "./lib/markdown-to-html";
import renderNav from "./lib/render-nav";
import generateIndexInfo from "./lib/generate-index-info";
import page from "./lib/render-page";
import mdR from "./lib/markdown-regex";
import { FileTree, StringFile } from "./lib/types";

const [docsFolder, outputFolder, ...argsRest] = process.argv.slice(2);

// Default parameters
const defaultFolder = "docs";
const folder = docsFolder || defaultFolder;
const output = (outputFolder) ? outputFolder : `_${folder}`;
const templateFilename = "template.html";
const contentsFilename = "contents.json";
const preferences = ["index.md", "README.md"];

// Guards
// Bail out if more than 1 args
if (argsRest && argsRest.length > 0) {
  console.error("Too may arguments");
  usage(true);
}

// Bail out if the folder doesn't exist
if (!sh.test("-e", folder)) {
  console.error(
    `Folder ${folder} not found at ${path.join(process.cwd(), folder)}`
  );
  usage(true);
}

// Define template html, user's first, otherwise default
let template = path.join(folder, templateFilename);
if (!sh.test("-e", template)) {
  template = path.join(__dirname, defaultFolder, templateFilename);
}
const tpl = sh.cat(template);

// Prepare output folder (create, clean, copy sources)
sh.mkdir("-p", output);
sh.rm("-rf", output + "/*");
sh.cp("-R", folder + `/!(node_modules|${output})`, output);

// Start processing. Outline:
//
// 1. Get all files
// 2. Sort them
// 3. Group them hierachically
// 4. Parse files and generate output html files

sh.cd(output);
const all = sh.find("*");

const mds = all
  .filter(file => file.match(mdR))
  .sort(sortByPreferences.bind(null, preferences))
  .map(file => {
    const content = sh.cat(file).toString(); // The result is a weird not-string
    return {
      path: file,
      url: mdUrl(file),
      content,
      html: md2html(content)
    };
  });

const groupedMds: FileTree<StringFile> = mds.reduce(
  (grouped: FileTree<StringFile>, value) => groupByPath(grouped, value.path),
  []
);

mds.forEach(({ path, url, html }) => {
  const navHtml = renderNav(generateIndexInfo(path, groupedMds));
  const pageHtml = page(tpl, navHtml, html);
  fs.writeFileSync(url, pageHtml);
});

const contentsJSON = {
  paths: groupedMds,
  contents: mds.map((md, i) => ({ ...md, id: i }))
};
fs.writeFileSync(contentsFilename, JSON.stringify(contentsJSON, null, 2));

sh.rm("-r", "**/*.md");

function usage(error: boolean) {
  console.log(
    `
Usage:

  markdown-folder-to-html [input-folder]

    input-folder [optional] defaults to \`docs\`
  `
  );
  process.exit(error ? 1 : 0);
}
@joakin
Copy link
Owner

joakin commented Aug 23, 2018

What are you trying to do? This tool doesn't make sense to be run on a root directory. It is made to take a directory of markdown files and other assets and give you back another one with the HTML and those assets. It is not meant to be run in the root of the documents folder as you have experienced.

I don't want to add special casing to the copying, because it will get out of hand. This tool will give you a folder like the one you specified, but with the HTMLs added.

Maybe if you really want to run it that way you can rm the folder after generation, but I'd suggest moving the documents to a folder, and calling the command with that folder.

@ronshe
Copy link
Author

ronshe commented Aug 28, 2018

I want to create a self contained documentation project. Installation of markdown-folder-to-htmland running it are included in the project package.json. The package.json includes the markdown-folder-to-html package. The problem is that in this case markdown-folder-to-html will look for MD files in node_modules too. The output folder is a subfolder of the project.

@ronshe
Copy link
Author

ronshe commented Sep 1, 2018

My changes enables creating an online GIT-BOOK, where all the MD files are located somewhere in your server and the output HTML is the website public_html folder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants