-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for Mermaid configuration options, version and style #5
base: master
Are you sure you want to change the base?
Changes from all commits
829a7e8
bdac0dc
3210e87
da2b372
b4e45b1
04f6ccb
e980017
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
const doctrine = require('doctrine') | ||
const env = require('jsdoc/env'); | ||
|
||
/** | ||
* @constant {string} JSDOC_MERMAID_TAG | ||
|
@@ -7,16 +8,16 @@ const doctrine = require('doctrine') | |
const JSDOC_MERMAID_TAG = /@mermaid\b/ | ||
|
||
/** | ||
* @constant {string} MERMAID_VERSION | ||
* @description Semver Mermaid version | ||
* @constant {string} MERMAID_HTML_SCRIPT | ||
* @description Html tag that includes mermaid library | ||
*/ | ||
const MERMAID_VERSION = '8.4.8' | ||
const MERMAID_HTML_SCRIPT = '<script src="https://unpkg.com/mermaid${MERMAID_VERSION}/dist/mermaid.min.js"></script>' | ||
|
||
/** | ||
* @constant {string} MERMAID_HTML_SCRIPT | ||
* @description Html tag that include mermaid library | ||
* @constant {string} MERMAID_INIT_SCRIPT | ||
* @description Html script that include initializes Mermaid with config options | ||
*/ | ||
const MERMAID_HTML_SCRIPT = `<script src="https://unpkg.com/mermaid@${MERMAID_VERSION}/dist/mermaid.min.js"></script>` | ||
const MERMAID_INIT_SCRIPT = '<script>mermaid.initialize(${MERMAID_CONFIG});</script>\n' | ||
|
||
/** | ||
* @constant {Object} ESCAPE_HTML_MAPPING | ||
|
@@ -30,6 +31,13 @@ const ESCAPE_HTML_MAPPING = { | |
"'": ''' | ||
} | ||
|
||
/** | ||
* @constant {object} MERMAID_CONFIG | ||
* @description Mermaid configuration options from the JSDoc configuration settings | ||
*/ | ||
const MERMAID_CONFIG = env.conf.mermaid || {}; | ||
|
||
|
||
function escapeHtml(str) { | ||
return str.replace(/[&<>"']/g, c => ESCAPE_HTML_MAPPING[c]) | ||
} | ||
|
@@ -45,17 +53,35 @@ exports.handlers = { | |
recoverable: true | ||
}).tags | ||
|
||
let style = MERMAID_CONFIG.style ? ' style="' + MERMAID_CONFIG.style + '"' : '' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cmidgley this is not-blocking, but we should prolly |
||
|
||
const htmls = tags.map(tag => [ | ||
'<div class="mermaid">', | ||
'<div class="mermaid"', | ||
style, | ||
'>', | ||
escapeHtml(tag.description), | ||
'</div>' | ||
].join('')) | ||
|
||
if (htmls) { | ||
e.doclet.description = e.doclet.description || '' | ||
|
||
if (!isAddedMermaid[e.doclet.memberof]) { | ||
e.doclet.description += MERMAID_HTML_SCRIPT | ||
if (!isAddedMermaid[e.doclet.memberof] && !MERMAID_CONFIG.disableScript) { | ||
let version = MERMAID_CONFIG.version ? '@' + MERMAID_CONFIG.version : '' | ||
|
||
// clone the Mermaid config so we can remove our unique options before rendering via JSON.stringify | ||
let cloneMermaidConfig = {...MERMAID_CONFIG} | ||
delete cloneMermaidConfig.version | ||
delete cloneMermaidConfig.style | ||
|
||
let MERMAID_VERSION = version; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this var is unused. |
||
|
||
const mermaidScript = tags.map(tag => [ | ||
MERMAID_HTML_SCRIPT.replace('${MERMAID_VERSION}', version), | ||
MERMAID_INIT_SCRIPT.replace('${MERMAID_CONFIG}', JSON.stringify(cloneMermaidConfig)), | ||
].join('')) | ||
e.doclet.description += mermaidScript | ||
|
||
isAddedMermaid[e.doclet.memberof] = true | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "jsdoc-mermaid", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. <3 |
||
"description": "A tool to automagically create flowcharts and diagrams in your jsdocs.", | ||
"main": "index.js", | ||
"scripts": { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the suggestion i'm making in my comment from #5
BEFORE:
AFTER:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cmidgley i'd think we can prolly include this change before merging. What are your thoughts?
I think we can slightly tweak it.