From 980e5d3ec38f93791fd1bf6a62db686773f8e3b8 Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Wed, 8 Nov 2017 21:59:54 +0000 Subject: [PATCH] Replace marked.js with markdown-it.js The markdown parser used in StoryBoard (marked.js) is buggy and mostly unmaintained. This commit replaces it with a better library which is actually maintained. There is a small change to functionality incurred with this change, we no longer automatically attempt to highlight every code block by guessing the language. Instead only fenced code blocks are highlighted, and then only if the language is specified. For example: ``` python def foo(): return 'foo' ``` Change-Id: I8ee562e3e97ab6f0d09a21c27907d1a0959aceb1 --- bower.json | 2 +- src/app/services/directive/markdown.js | 15 +++++++++++++-- src/app/storyboard/module.js | 7 ------- src/index.html | 2 +- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/bower.json b/bower.json index 28733ca4..f9daf28e 100644 --- a/bower.json +++ b/bower.json @@ -14,7 +14,7 @@ "angular-moment": "0.9.0", "angular-cache": "3.2.5", "angularjs-viewhead": "0.0.1", - "marked": "0.3.5", + "markdown-it": "8.4.0", "moment-timezone": "0.5.4", "highlightjs": "9.1.0", "ng-sortable": "1.3.1" diff --git a/src/app/services/directive/markdown.js b/src/app/services/directive/markdown.js index f39e190f..84f8aaac 100644 --- a/src/app/services/directive/markdown.js +++ b/src/app/services/directive/markdown.js @@ -18,9 +18,19 @@ * Service for rendering text as markdown. */ angular.module('sb.services') - .directive('insertMarkdown', function($sanitize) { + .directive('insertMarkdown', function($sanitize, $window) { 'use strict'; + var md = $window.markdownit({ + html: true, + highlight: function(code, lang) { + if (lang && $window.hljs.getLanguage(lang)) { + return $window.hljs.highlight(lang, code, true).value; + } + return ''; // Don't highlight if no language specified + } + }); + return { restrict: 'E', scope: { @@ -28,7 +38,8 @@ angular.module('sb.services') }, link: function(scope, elem) { scope.$watch('content', function(newVal) { - elem.html('
' + $sanitize(marked(newVal)) + '
'); + var html = md.render(newVal); + elem.html('
' + $sanitize(html) + '
'); }, true); } }; diff --git a/src/app/storyboard/module.js b/src/app/storyboard/module.js index 180aac44..f3424f95 100644 --- a/src/app/storyboard/module.js +++ b/src/app/storyboard/module.js @@ -63,13 +63,6 @@ angular.module('storyboard', preferences: PreferenceResolver.resolvePreferences } }); - - // Set up syntax highlighting for the markdown parser - marked.setOptions({ - highlight: function (code) { - return hljs.highlightAuto(code).value; - } - }); }) .run(function ($log, $rootScope, $document) { 'use strict'; diff --git a/src/index.html b/src/index.html index 7dae6d21..70d31c04 100644 --- a/src/index.html +++ b/src/index.html @@ -40,7 +40,7 @@ - +