From f0d2fb53d4de95244834ffe5d444768db930f057 Mon Sep 17 00:00:00 2001 From: Dick Choi Date: Wed, 26 Oct 2016 13:06:02 +0900 Subject: [PATCH] add sequence diagram --- browser/components/MarkdownPreview.js | 31 +++++++++++++++++++++------ browser/components/markdown.styl | 9 +++++++- browser/lib/markdown.js | 9 +++++--- lib/main.html | 7 ++++-- oh-my-cdn.json | 3 ++- webpack-skeleton.js | 3 ++- 6 files changed, 48 insertions(+), 14 deletions(-) diff --git a/browser/components/MarkdownPreview.js b/browser/components/MarkdownPreview.js index 603b7315..7a1f6815 100644 --- a/browser/components/MarkdownPreview.js +++ b/browser/components/MarkdownPreview.js @@ -5,6 +5,7 @@ import CodeMirror from 'codemirror' import consts from 'browser/lib/consts' import Raphael from 'raphael' import flowchart from 'flowchart' +import SequenceDiagram from 'sequence-diagram' function decodeHTMLEntities (text) { var entities = [ @@ -213,12 +214,12 @@ export default class MarkdownPreview extends React.Component { }) }) let opts = {} - if (this.props.theme === 'dark') { - opts['font-color'] = '#DDD' - opts['line-color'] = '#DDD' - opts['element-color'] = '#DDD' - opts['fill'] = '#3A404C' - } + // if (this.props.theme === 'dark') { + // opts['font-color'] = '#DDD' + // opts['line-color'] = '#DDD' + // opts['element-color'] = '#DDD' + // opts['fill'] = '#3A404C' + // } _.forEach(this.refs.root.contentWindow.document.querySelectorAll('.flowchart'), (el) => { Raphael.setWindow(this.getWindow()) try { @@ -230,6 +231,24 @@ export default class MarkdownPreview extends React.Component { }) } catch (e) { console.error(e) + el.className = 'flowchart-error' + el.innerHTML = 'Flowchart parse error: ' + e.message + } + }) + + _.forEach(this.refs.root.contentWindow.document.querySelectorAll('.sequence'), (el) => { + Raphael.setWindow(this.getWindow()) + try { + let diagram = SequenceDiagram.parse(decodeHTMLEntities(el.innerHTML)) + el.innerHTML = '' + diagram.drawSVG(el, {theme: 'simple'}) + _.forEach(el.querySelectorAll('a'), (el) => { + el.addEventListener('click', this.anchorClickHandler) + }) + } catch (e) { + console.error(e) + el.className = 'sequence-error' + el.innerHTML = 'Sequence diagram parse error: ' + e.message } }) } diff --git a/browser/components/markdown.styl b/browser/components/markdown.styl index d572e29b..5beb7210 100644 --- a/browser/components/markdown.styl +++ b/browser/components/markdown.styl @@ -68,6 +68,12 @@ body padding 5px margin -5px border-radius 5px + .flowchart-error, .sequence-error + background-color errorBackgroundColor + color errorTextColor + padding 5px + border-radius 5px + justify-content left li label.taskListItem margin-left -2em @@ -202,9 +208,10 @@ pre margin 0 0 1em display flex line-height 1.4em - &.flowchart + &.flowchart, &.sequence display flex justify-content center + background-color white &.CodeMirror height initial &>code diff --git a/browser/lib/markdown.js b/browser/lib/markdown.js index 6af8f0b0..b5e4c893 100644 --- a/browser/lib/markdown.js +++ b/browser/lib/markdown.js @@ -23,6 +23,9 @@ var md = markdownit({ if (lang === 'flowchart') { return `
${str}
` } + if (lang === 'sequence') { + return `
${str}
` + } return '
' +
     createGutter(str) +
     '' +
@@ -132,13 +135,13 @@ function strip (input) {
       .replace(/`{3}.*\n/g, '')
       .replace(/<(.*?)>/g, '$1')
       .replace(/^[=\-]{2,}\s*$/g, '')
-      .replace(/\[\^.+?\](\: .*?$)?/g, '')
+      .replace(/\[\^.+?\](: .*?$)?/g, '')
       .replace(/\s{0,2}\[.*?\]: .*?$/g, '')
-      .replace(/\!\[.*?\][\[\(].*?[\]\)]/g, '')
+      .replace(/!\[.*?\][\[\(].*?[\]\)]/g, '')
       .replace(/\[(.*?)\][\[\(].*?[\]\)]/g, '$1')
       .replace(/>/g, '')
       .replace(/^\s{1,2}\[(.*?)\]: (\S+)( ".*?")?\s*$/g, '')
-      .replace(/^\#{1,6}\s*([^#]*)\s*(\#{1,6})?/gm, '$1')
+      .replace(/^#{1,6}\s*([^#]*)\s*(#{1,6})?/gm, '$1')
       .replace(/([\*_]{1,3})(\S.*?\S)\1/g, '$2')
       .replace(/(`{3,})(.*?)\1/gm, '$2')
       .replace(/^-{3,}\s*$/g, '')
diff --git a/lib/main.html b/lib/main.html
index c1acfcb8..802a6f42 100644
--- a/lib/main.html
+++ b/lib/main.html
@@ -63,6 +63,10 @@
 
   
   
+  
+  
 
   
   
@@ -72,8 +76,7 @@