From 89ed8fe75fceb6bcd64d0846b88521580e061434 Mon Sep 17 00:00:00 2001 From: Andrew Turner Date: Sat, 16 Jan 2016 12:01:45 -0600 Subject: [PATCH] version link to API docs --- generate-docs.js | 34 ++++++++++++++++++++++++++++++++++ generate-readme-toc.js | 29 ----------------------------- package.json | 4 ++-- 3 files changed, 36 insertions(+), 31 deletions(-) create mode 100644 generate-docs.js delete mode 100644 generate-readme-toc.js diff --git a/generate-docs.js b/generate-docs.js new file mode 100644 index 0000000..911f46b --- /dev/null +++ b/generate-docs.js @@ -0,0 +1,34 @@ +'use strict' + +const Toc = require('markdown-toc') +const Fs = require('fs') +const Package = require('./package.json') + +const internals = { + api: { + filename: './API.md', + contents: Fs.readFileSync('./API.md', 'utf8') + }, + readme: { + filename: './README.md', + contents: Fs.readFileSync('./README.md', 'utf8') + } +} + +const tocOptions = { + bullets: '-', + slugify: function (text) { + return text.toLowerCase() + .replace(/\s/g, '-') + .replace(/[^\w-]/g, '') + } +} + +// generate table of contents and version label docs +const api = Toc.insert(internals.api.contents, tocOptions) + .replace(/(.|\n)*/, `\nAPI Documentation - \`v${Package.version}\`\n---\n`) +Fs.writeFileSync(internals.api.filename, api) + +// create absolute URL for versioned docs +const readme = internals.readme.contents.replace(/\[API Documentation\]\(.*\)/g, `[API Documentation](${Package.homepage || ''}/blob/v${Package.version}/${internals.api.filename})`) +Fs.writeFileSync(internals.readme.filename, readme) diff --git a/generate-readme-toc.js b/generate-readme-toc.js deleted file mode 100644 index 423b63a..0000000 --- a/generate-readme-toc.js +++ /dev/null @@ -1,29 +0,0 @@ -'use strict' -// from https://github.com/hapijs/joi/blob/master/generate-readme-toc.js - -const Toc = require('markdown-toc') -const Fs = require('fs') -const Package = require('./package.json') - -const internals = { - filename: './API.md' -} - -internals.generate = function () { - const api = Fs.readFileSync(internals.filename, 'utf8') - const tocOptions = { - bullets: '-', - slugify: function (text) { - return text.toLowerCase() - .replace(/\s/g, '-') - .replace(/[^\w-]/g, '') - } - } - - const output = Toc.insert(api, tocOptions) - .replace(/(.|\n)*/, '\nAPI Documentation - `v' + Package.version + '`\n---\n') - - Fs.writeFileSync(internals.filename, output) -} - -internals.generate() diff --git a/package.json b/package.json index 76b0a84..ddc4d51 100644 --- a/package.json +++ b/package.json @@ -8,8 +8,8 @@ "test": "npm run lint && lab -r console -t 100 -a code", "test-ci": "standard && lab -r lcov -t 100 -a code | ./node_modules/.bin/coveralls", "test-cov-html": "lab -r html -o coverage.html", - "toc": "node generate-readme-toc.js", - "version": "npm run toc && git add API.md" + "generate-docs": "node generate-docs.js", + "version": "npm run generate-docs && git add README.md API.md" }, "engines": { "node": ">= 4.0.0"