Skip to content

Instantly share code, notes, and snippets.

@waldekmastykarz
Created September 2, 2020 17:44
Show Gist options
  • Select an option

  • Save waldekmastykarz/cc5f15f31092d8ce790009ea63f266e9 to your computer and use it in GitHub Desktop.

Select an option

Save waldekmastykarz/cc5f15f31092d8ce790009ea63f266e9 to your computer and use it in GitHub Desktop.
Reformat docs
const fs = require('fs');
const os = require('os');
const path = require('path');
function readdirR(dir) {
return fs.statSync(dir).isDirectory()
? Array.prototype.concat(...fs.readdirSync(dir).map(f => readdirR(path.join(dir, f))))
: dir;
};
const docsPath = '/Users/waldek/github/waldekmastykarz/office365-cli/docs/manual/docs/cmd';
const files = readdirR(docsPath);
const optionRegex = /([^|]+)\|(.*)\n/g;
files.forEach(file => {
if (!file.endsWith('.md')) {
return;
}
let md = fs.readFileSync(file, 'utf8');
// store max position after which strings shouldn't be replaced
let stopPos = md.indexOf('## Remarks');
if (stopPos === -1) {
stopPos = md.indexOf('## Examples');
}
if (stopPos === -1) {
stopPos = md.length;
}
// remove table header
md = md.replace(`Option|Description${os.EOL}`, '');
md = md.replace(`------|-----------${os.EOL}`, '');
const options = [...md.matchAll(optionRegex)];
for (let i = 0; i < options.length; i++) {
if (md.indexOf(options[0]) > stopPos) {
break;
}
const option = options[i];
const newLines = i === options.length-1 ? os.EOL : `${os.EOL}${os.EOL}`;
md = md.replace(option[0], `${option[1].trim()}${os.EOL}: ${option[2].trim()}${newLines}`);
}
fs.writeFileSync(file, md, 'utf8');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment