From ddb75866b91670eb0671153a78d7f1ab29179bd3 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sun, 10 Jan 2016 09:41:07 +0300 Subject: [PATCH] Demo: mpre highlight langs + auto mode + debug info --- package.json | 7 ++- support/demo_template/index.jade | 2 - support/demo_template/index.js | 90 +++++++++++++++++++++++++++----- 3 files changed, 81 insertions(+), 18 deletions(-) diff --git a/package.json b/package.json index ff7f2c9..ead1655 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,9 @@ "repository": "markdown-it/markdown-it", "license": "MIT", "main": "index.js", - "bin": { "markdown-it": "bin/markdown-it.js" }, + "bin": { + "markdown-it": "bin/markdown-it.js" + }, "scripts": { "test": "make test" }, @@ -41,6 +43,7 @@ "coveralls": "~2.11.6", "eslint": "0.24.0", "eslint-plugin-nodeca": "^1.0.0", + "highlight.js": "^9.1.0", "istanbul": "*", "jade": "~1.11.0", "markdown-it-abbr": "^1.0.2", @@ -55,8 +58,8 @@ "markdown-it-sup": "^1.0.0", "markdown-it-testgen": "~0.1.3", "marked": "0.3.5", - "stylus": "~0.53.0", "mocha": "*", + "stylus": "~0.53.0", "uglify-js": "*" } } diff --git a/support/demo_template/index.jade b/support/demo_template/index.jade index 6dc8a2f..5344eaf 100644 --- a/support/demo_template/index.jade +++ b/support/demo_template/index.jade @@ -14,8 +14,6 @@ html script(src='https://cdn.jsdelivr.net/bootstrap/3.2.0/js/bootstrap.min.js') link(rel='stylesheet' href='https://cdn.jsdelivr.net/bootstrap/3.2.0/css/bootstrap.css') - script(src='https://cdn.jsdelivr.net/highlight.js/9.1.0/highlight.min.js') - script(src='https://cdn.jsdelivr.net/highlight.js/9.1.0/languages/stylus.min.js') link(rel='stylesheet' href='https://cdn.jsdelivr.net/highlight.js/9.1.0/styles/github.min.css') script(src='markdown-it.js') diff --git a/support/demo_template/index.js b/support/demo_template/index.js index b2fc21e..0081f6a 100644 --- a/support/demo_template/index.js +++ b/support/demo_template/index.js @@ -5,6 +5,63 @@ var mdurl = require('mdurl'); + +var hljs = require('highlight.js/lib/highlight.js'); + +hljs.registerLanguage('actionscript', require('highlight.js/lib/languages/actionscript')); +hljs.registerLanguage('apache', require('highlight.js/lib/languages/apache')); +hljs.registerLanguage('arduino', require('highlight.js/lib/languages/arduino')); +hljs.registerLanguage('armasm', require('highlight.js/lib/languages/armasm')); +hljs.registerLanguage('xml', require('highlight.js/lib/languages/xml')); +hljs.registerLanguage('asciidoc', require('highlight.js/lib//languages/asciidoc')); +hljs.registerLanguage('avrasm', require('highlight.js/lib/languages/avrasm')); +hljs.registerLanguage('bash', require('highlight.js/lib/languages/bash')); +hljs.registerLanguage('clojure', require('highlight.js/lib/languages/clojure')); +hljs.registerLanguage('cmake', require('highlight.js/lib/languages/cmake')); +hljs.registerLanguage('coffeescript', require('highlight.js/lib/languages/coffeescript')); +hljs.registerLanguage('cpp', require('highlight.js/lib/languages/cpp')); +hljs.registerLanguage('css', require('highlight.js/lib/languages/css')); +hljs.registerLanguage('diff', require('highlight.js/lib/languages/diff')); +hljs.registerLanguage('django', require('highlight.js/lib/languages/django')); +hljs.registerLanguage('dockerfile', require('highlight.js/lib/languages/dockerfile')); +hljs.registerLanguage('ruby', require('highlight.js/lib/languages/ruby')); +hljs.registerLanguage('fortran', require('highlight.js/lib/languages/fortran')); +hljs.registerLanguage('glsl', require('highlight.js/lib/languages/glsl')); +hljs.registerLanguage('go', require('highlight.js/lib/languages/go')); +hljs.registerLanguage('groovy', require('highlight.js/lib/languages/groovy')); +hljs.registerLanguage('handlebars', require('highlight.js/lib/languages/handlebars')); +hljs.registerLanguage('haskell', require('highlight.js/lib/languages/haskell')); +hljs.registerLanguage('ini', require('highlight.js/lib/languages/ini')); +hljs.registerLanguage('java', require('highlight.js/lib/languages/java')); +hljs.registerLanguage('javascript', require('highlight.js/lib/languages/javascript')); +hljs.registerLanguage('json', require('highlight.js/lib/languages/json')); +hljs.registerLanguage('less', require('highlight.js/lib/languages/less')); +hljs.registerLanguage('lisp', require('highlight.js/lib/languages/lisp')); +hljs.registerLanguage('livescript', require('highlight.js/lib/languages/livescript')); +hljs.registerLanguage('lua', require('highlight.js/lib/languages/lua')); +hljs.registerLanguage('makefile', require('highlight.js/lib/languages/makefile')); +hljs.registerLanguage('matlab', require('highlight.js/lib/languages/matlab')); +hljs.registerLanguage('mipsasm', require('highlight.js/lib/languages/mipsasm')); +hljs.registerLanguage('perl', require('highlight.js/lib/languages/perl')); +hljs.registerLanguage('nginx', require('highlight.js/lib/languages/nginx')); +hljs.registerLanguage('objectivec', require('highlight.js/lib/languages/objectivec')); +hljs.registerLanguage('php', require('highlight.js/lib/languages/php')); +hljs.registerLanguage('python', require('highlight.js/lib/languages/python')); +hljs.registerLanguage('rust', require('highlight.js/lib/languages/rust')); +hljs.registerLanguage('scala', require('highlight.js/lib/languages/scala')); +hljs.registerLanguage('scheme', require('highlight.js/lib/languages/scheme')); +hljs.registerLanguage('scss', require('highlight.js/lib/languages/scss')); +hljs.registerLanguage('smalltalk', require('highlight.js/lib/languages/smalltalk')); +hljs.registerLanguage('stylus', require('highlight.js/lib/languages/stylus')); +hljs.registerLanguage('swift', require('highlight.js/lib/languages/swift')); +hljs.registerLanguage('tcl', require('highlight.js/lib/languages/tcl')); +hljs.registerLanguage('tex', require('highlight.js/lib/languages/tex')); +hljs.registerLanguage('typescript', require('highlight.js/lib/languages/typescript')); +hljs.registerLanguage('verilog', require('highlight.js/lib/languages/verilog')); +hljs.registerLanguage('vhdl', require('highlight.js/lib/languages/vhdl')); +hljs.registerLanguage('yaml', require('highlight.js/lib/languages/yaml')); + + var mdHtml, mdSrc, permalink, scrollMap; var defaults = { @@ -22,25 +79,30 @@ var defaults = { }; defaults.highlight = function (str, lang) { - if (!defaults._highlight || !window.hljs) { return ''; } + var esc = mdHtml.utils.escapeHtml; + + try { + if (!defaults._highlight) { + throw 'highlighting disabled'; + } - var hljs = window.hljs, - esc = mdHtml.utils.escapeHtml; + if (lang && lang !== 'auto' && hljs.getLanguage(lang)) { - if (lang && hljs.getLanguage(lang)) { - try { - return '
' +
+      return '
' +
              hljs.highlight(lang, str).value +
              '
'; - } catch (__) { /**/ } - } - /* try { - var result = hljs.highlightAuto(str); - return '
' +
-           result.value +
-           '
'; - } catch (__) {} */ + } else if (lang === 'auto') { + + var result = hljs.highlightAuto(str); + + console.log('highlight language: ' + result.language + ', relevance: ' + result.relevance); + + return '
' +
+             result.value +
+             '
'; + } + } catch (__) { /**/ } return '
' + esc(str) + '
'; };