pimd
Version:
Processing instructions for Markdown
69 lines (61 loc) • 1.68 kB
JavaScript
const MarkdownIt = require("markdown-it")
const Config = require("./config")
const Contents = require("./contents")
const Hooks = require("./hooks")
const processingInstructionBlock = require("./rules-block/processing-instruction")
const processingInstructionInline = require("./rules-inline/processing-instruction")
module.exports = class Document {
constructor(source, config) {
this.source = source
this.config = config || new Config()
this.title = null
this.markdown = new MarkdownIt(this.config.markdown)
this.extendMarkdown()
this.tokenize()
this.contents = new Contents()
}
get hooks() {
this._hooks = this._hooks || new Hooks(this.config.hooks)
return this._hooks
}
extendMarkdown() {
this.markdown.block.ruler.before(
"html_block",
"processing_instruction_block",
processingInstructionBlock,
{}
)
this.markdown.inline.ruler.before(
"html_inline",
"processing_instruction_inline",
processingInstructionInline,
{}
)
}
tokenize() {
this.markdown.renderer = new this.config.Renderer(this)
this.tokens = this.markdown.parse(this.source, {})
this.tokens.forEach((token, i) => {
if (token.type === "heading_open") {
if (!this.title && token.markup === "#") {
this.title = this.tokens[i + 1].content
}
}
})
}
render() {
const html = this.markdown.renderer.render(
this.tokens,
this.markdown.options,
this
)
return html
}
renderDocument() {
return this.markdown.renderer.renderDocument(
this.tokens,
this.markdown.options,
this
)
}
}