From 7d2b7f094e3e6299cc4d1d5ec0240dc382428012 Mon Sep 17 00:00:00 2001 From: Harminder Virk Date: Sun, 18 Sep 2016 23:11:30 +0530 Subject: [PATCH] feat(parser): add asciidoc parser asciidoc parser to convert ascii doc files to html --- package.json | 1 + src/AsciiDoc/index.js | 40 ++++++++++++++++++++++++++++++++++++++++ test/ascii.spec.js | 40 ++++++++++++++++++++++++++++++++++++++++ test/docs/abc.adoc | 15 +++++++++++++++ test/readers.spec.js | 4 ++-- 5 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 src/AsciiDoc/index.js create mode 100644 test/ascii.spec.js create mode 100644 test/docs/abc.adoc diff --git a/package.json b/package.json index aa50778..2a90964 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "author": "adonisjs", "license": "MIT", "dependencies": { + "asciidoctor.js": "^1.5.5-2", "cat-log": "^1.0.0", "fs-extra": "^0.30.0", "gray-matter": "^2.0.2", diff --git a/src/AsciiDoc/index.js b/src/AsciiDoc/index.js new file mode 100644 index 0000000..9ad524f --- /dev/null +++ b/src/AsciiDoc/index.js @@ -0,0 +1,40 @@ +'use strict' + +/* + * docketjs + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +const asciidoctor = require('asciidoctor.js')() +const matter = require('gray-matter') +const opal = asciidoctor.Opal +const processor = asciidoctor.Asciidoctor(true) + +class AsciiDoc { + + constructor (attributes) { + attributes = attributes || ['icon=font', 'skip-front-matter', 'sectlinks', 'sectanchors', 'toc=macro'] + this.options = opal.hash({doctype: 'article', attributes}) + } + + /** + * Converts an ascii doc to HTML. + * + * @param {String} content + * + * @return {Object} + */ + convert (content) { + const doc = processor.$load(content, this.options) + const frontMatter = doc.$attr('front-matter') + const meta = typeof (frontMatter) === 'string' ? matter(`---\n${frontMatter}\n---`) : {} + return {meta: meta.data, html: doc.$content()} + } + +} + +module.exports = AsciiDoc diff --git a/test/ascii.spec.js b/test/ascii.spec.js new file mode 100644 index 0000000..d667e96 --- /dev/null +++ b/test/ascii.spec.js @@ -0,0 +1,40 @@ +'use strict' + +/* + * docketjs + * + * (c) Harminder Virk + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. +*/ + +const chai = require('chai') +const expect = chai.expect +const path = require('path') +const fs = require('fs') +const cheerio = require('cheerio') +const AsciiDoc = require('../src/AsciiDoc') + +describe('AsciiDoc', function () { + it('should be able to parse ascii doc string to html', function () { + const asciidoc = new AsciiDoc() + const asciiFile = path.join(__dirname, './docs/abc.adoc') + const doc = asciidoc.convert(fs.readFileSync(asciiFile, 'utf-8')) + expect(doc).to.have.property('html') + expect(doc).to.have.property('meta') + expect(doc.meta.permalink).to.equal('hello-world') + expect(doc.meta.title).to.equal('Hello World') + expect(doc.html).to.be.a('string') + }) + + it('should be generate toc for the document', function () { + const asciidoc = new AsciiDoc() + const asciiFile = path.join(__dirname, './docs/abc.adoc') + const doc = asciidoc.convert(fs.readFileSync(asciiFile, 'utf-8')) + expect(doc).to.have.property('html') + expect(doc).to.have.property('meta') + const $ = cheerio.load(doc.html) + expect($('#toctitle')).to.have.length(1) + }) +}) diff --git a/test/docs/abc.adoc b/test/docs/abc.adoc new file mode 100644 index 0000000..402035b --- /dev/null +++ b/test/docs/abc.adoc @@ -0,0 +1,15 @@ +--- +title: Hello World +permalink: hello-world +--- + +toc::[] + +== Hello World +This is a sample abc file. + +=== A Nested Heading +This has some related content + +== A top Level One Again +I am the king diff --git a/test/readers.spec.js b/test/readers.spec.js index 9589ecf..47fc9fd 100644 --- a/test/readers.spec.js +++ b/test/readers.spec.js @@ -19,7 +19,7 @@ require('co-mocha') describe('Readers', function () { context('Fs Reader', function () { it('should return an array of all markdown files from a given directory', function * () { - const fsReader = new FSReader(path.join(__dirname, './docs')) + const fsReader = new FSReader(path.join(__dirname, './docs'), ['.md']) const docs = yield fsReader.getDocs() expect(docs).to.be.an('array') expect(docs.length).to.equal(3) @@ -30,7 +30,7 @@ describe('Readers', function () { }) it('should be able to define doc extensions for the files to be returned', function * () { - const fsReader = new FSReader(path.join(__dirname, './docs'), ['.adoc']) + const fsReader = new FSReader(path.join(__dirname, './docs'), ['.aciidoc']) const docs = yield fsReader.getDocs() expect(docs).to.be.an('array') expect(docs.length).to.equal(0)