No manual global mermaid

This commit is contained in:
Tyler Long
2017-09-09 21:47:21 +08:00
parent 5da8c26cb6
commit fc5c07027c
6 changed files with 46 additions and 118 deletions

View File

@@ -1,9 +1,9 @@
// Karma configuration // Karma configuration
// Generated on Mon Nov 03 2014 07:53:38 GMT+0100 (CET) // Generated on Mon Nov 03 2014 07:53:38 GMT+0100 (CET)
import { webConfig } from './webpack.config.base.js' import { jsConfig } from './webpack.config.base.js'
const webpackConfig = webConfig() const webpackConfig = jsConfig()
module.exports = function (config) { module.exports = function (config) {
config.set({ config.set({

View File

@@ -1,8 +1,8 @@
{ {
"name": "mermaid", "name": "mermaid",
"version": "7.0.10", "version": "7.0.11",
"description": "Markdownish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.", "description": "Markdownish syntax for generating flowcharts, sequence diagrams, class diagrams, gantt charts and git graphs.",
"main": "src/mermaid.js", "main": "dist/mermaid.core.js",
"keywords": [ "keywords": [
"diagram", "diagram",
"markdown", "markdown",

View File

@@ -9,7 +9,6 @@ var log = Logger.Log
var mermaidAPI = require('./mermaidAPI') var mermaidAPI = require('./mermaidAPI')
var nextId = 0 var nextId = 0
module.exports.mermaidAPI = mermaidAPI
/** /**
* ## init * ## init
* Function that goes through the document to find the chart definitions in there and render them. * Function that goes through the document to find the chart definitions in there and render them.
@@ -110,23 +109,11 @@ var init = function () {
} }
} }
module.exports.init = init const version = function () {
module.exports.parse = mermaidAPI.parse
/**
* ## version
* Function returning version information
* @returns {string} A string containing the version info
*/
module.exports.version = function () {
return 'v' + require('../package.json').version return 'v' + require('../package.json').version
} }
/** const initialize = function (config) {
* ## initialize
* This function overrides the default configuration.
* @param config
*/
module.exports.initialize = function (config) {
log.debug('Initializing mermaid') log.debug('Initializing mermaid')
if (typeof config.mermaid !== 'undefined') { if (typeof config.mermaid !== 'undefined') {
if (typeof config.mermaid.startOnLoad !== 'undefined') { if (typeof config.mermaid.startOnLoad !== 'undefined') {
@@ -139,65 +126,16 @@ module.exports.initialize = function (config) {
mermaidAPI.initialize(config) mermaidAPI.initialize(config)
} }
var equals = function (val, variable) {
if (typeof variable === 'undefined') {
return false
} else {
return (val === variable)
}
}
/**
* Global mermaid object. Contains the functions:
* * init
* * initialize
* * version
* * parse
* * parseError
* * render
*/
global.mermaid = {
startOnLoad: true,
htmlLabels: true,
init: function () {
init.apply(null, arguments)
},
initialize: function (config) {
module.exports.initialize(config)
},
version: function () {
return mermaidAPI.version()
},
parse: function (text) {
return mermaidAPI.parse(text)
},
parseError: function (err) {
log.debug('Mermaid Syntax error:')
log.debug(err)
},
render: function (id, text, callback, element) {
return mermaidAPI.render(id, text, callback, element)
}
}
/**
* ## parseError
* This function overrides the default configuration.
* @param config
*/
module.exports.parseError = global.mermaid.parseError
/** /**
* ##contentLoaded * ##contentLoaded
* Callback function that is called when page is loaded. This functions fetches configuration for mermaid rendering and * Callback function that is called when page is loaded. This functions fetches configuration for mermaid rendering and
* calls init for rendering the mermaid diagrams on the page. * calls init for rendering the mermaid diagrams on the page.
*/ */
module.exports.contentLoaded = function () { const contentLoaded = function () {
var config var config
// Check state of start config mermaid namespace // Check state of start config mermaid namespace
if (typeof global.mermaid_config !== 'undefined') { if (typeof global.mermaid_config !== 'undefined') {
if (equals(false, global.mermaid_config.htmlLabels)) { if (global.mermaid_config.htmlLabels === false) {
global.mermaid.htmlLabels = false global.mermaid.htmlLabels = false
} }
} }
@@ -206,7 +144,7 @@ module.exports.contentLoaded = function () {
// For backwards compatability reasons also check mermaid_config variable // For backwards compatability reasons also check mermaid_config variable
if (typeof global.mermaid_config !== 'undefined') { if (typeof global.mermaid_config !== 'undefined') {
// Check if property startOnLoad is set // Check if property startOnLoad is set
if (equals(true, global.mermaid_config.startOnLoad)) { if (global.mermaid_config.startOnLoad === true) {
global.mermaid.init() global.mermaid.init()
} }
} else { } else {
@@ -232,6 +170,27 @@ if (typeof document !== 'undefined') {
* Wait for document loaded before starting the execution * Wait for document loaded before starting the execution
*/ */
window.addEventListener('load', function () { window.addEventListener('load', function () {
module.exports.contentLoaded() contentLoaded()
}, false) }, false)
} }
const mermaid = {
mermaidAPI,
startOnLoad: true,
htmlLabels: true,
init,
initialize,
version,
parse: mermaidAPI.parse,
parseError: function (err) {
log.debug('Mermaid Syntax error:')
log.debug(err)
},
render: mermaidAPI.render,
contentLoaded
}
export default mermaid

View File

@@ -5,7 +5,9 @@
/** /**
* Created by knut on 14-11-23. * Created by knut on 14-11-23.
*/ */
var mermaid = require('./mermaid') import mermaid from './mermaid'
global.mermaid = mermaid
describe('when using mermaid and ', function () { describe('when using mermaid and ', function () {
describe('when detecting chart type ', function () { describe('when detecting chart type ', function () {

View File

@@ -1,5 +1,4 @@
import path from 'path' import path from 'path'
import nodeExternals from 'webpack-node-externals'
import ExtractTextPlugin from 'extract-text-webpack-plugin' import ExtractTextPlugin from 'extract-text-webpack-plugin'
const rules = [ const rules = [
@@ -11,7 +10,7 @@ const rules = [
} }
] ]
export const webConfig = () => { export const jsConfig = () => {
return { return {
target: 'web', target: 'web',
entry: { entry: {
@@ -20,7 +19,10 @@ export const webConfig = () => {
externals: ['fs'], externals: ['fs'],
output: { output: {
path: path.join(__dirname, './dist/'), path: path.join(__dirname, './dist/'),
filename: '[name].js' filename: '[name].js',
library: 'mermaid',
libraryTarget: 'umd',
libraryExport: 'default'
}, },
module: { module: {
rules: rules.concat([ rules: rules.concat([
@@ -46,41 +48,6 @@ export const webConfig = () => {
} }
} }
export const nodeConfig = () => {
return {
target: 'node',
entry: {
mermaidAPI: './src/mermaidAPI.js'
},
externals: [nodeExternals()],
output: {
path: path.join(__dirname, './dist/'),
filename: '[name].js',
libraryTarget: 'commonjs2'
},
module: {
rules: rules.concat([
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: [
['env', {
'targets': {
'node': '6.9'
}
}]
]
}
}
}
])
}
}
}
export const lessConfig = () => { export const lessConfig = () => {
return { return {
target: 'web', target: 'web',

View File

@@ -1,13 +1,13 @@
import { webConfig, nodeConfig, lessConfig } from './webpack.config.base.js' import nodeExternals from 'webpack-node-externals'
const config = webConfig() import { jsConfig, lessConfig } from './webpack.config.base.js'
const slimConfig = webConfig() const config = jsConfig()
slimConfig.externals = ['fs', 'd3']
slimConfig.output.filename = '[name].slim.js'
const apiConfig = nodeConfig() const coreConfig = jsConfig()
coreConfig.externals = [nodeExternals(), 'fs']
coreConfig.output.filename = '[name].core.js'
const cssConfig = lessConfig() const cssConfig = lessConfig()
export default [config, slimConfig, apiConfig, cssConfig] export default [config, coreConfig, cssConfig]