Skip to content

Instantly share code, notes, and snippets.

@endiliey
Last active May 17, 2019 11:54
Show Gist options
  • Save endiliey/8970281b25b125747c27d8d2be9260f1 to your computer and use it in GitHub Desktop.
Save endiliey/8970281b25b125747c27d8d2be9260f1 to your computer and use it in GitHub Desktop.
Simple node script to generate random GitHub flavored valid markdown
const globby = require("globby");
const fs = require("fs-extra");
const path = require("path");
const fetch = require("node-fetch");
function randomName(length) {
let result = "";
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
async function randomMarkdown() {
const url = "https://jaspervdj.be/lorem-markdownum/markdown.txt";
try {
const response = await fetch(url);
const text = await response.text();
return text;
} catch (error) {
console.log(error);
}
}
async function insertContent(target) {
const docsDir = path.resolve(target || 'docs');
const docsFiles = await globby(["**"], {
cwd: docsDir
});
await Promise.all(
docsFiles.map(async source => {
const fromPath = path.resolve(docsDir, source);
const content = await randomMarkdown();
await fs.writeFile(fromPath, content);
})
);
}
async function createManyFile(length = 20, target) {
const docsDir = path.resolve(target || 'docs')
for (let i = 0; i < length; i++) {
const toPath = path.resolve(docsDir, `${randomName(10)}.md`)
await fs.writeFile(toPath, `${Math.random()}`);
}
}
async function rename(target) {
const docsDir = path.resolve(target || 'docs');
const docsFiles = await globby(["**"], {
cwd: docsDir
});
await Promise.all(
docsFiles.map(async source => {
const fromPath = path.resolve(docsDir, source);
const toPath = path.resolve(docsDir, `${randomName(10)}.md`);
await fs.rename(fromPath, toPath);
})
);
}
async function execute() {
await createManyFile(100);
await insertContent();
}
execute();
@endiliey
Copy link
Author

Result:

random markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment