This repository has been archived by the owner on Sep 5, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
asciidoc parser to convert ascii doc files to html
- Loading branch information
1 parent
3897655
commit 7d2b7f0
Showing
5 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
/* | ||
* docketjs | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
'use strict' | ||
|
||
/* | ||
* docketjs | ||
* | ||
* (c) Harminder Virk <virk@adonisjs.com> | ||
* | ||
* 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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters