@@ -14,7 +14,7 @@ import isURL from 'validator/lib/isURL'
1414import { transform } from 'markmap-lib/dist/transform'
1515import { Markmap } from 'markmap-lib/dist/view'
1616
17- import { stripTags } from '../.. /utils/string'
17+ import * as stringUtils from './lib /utils/string' // Import namespace
1818
1919import getUIElements from './lib/editor/ui-elements'
2020import { emojifyImageDir } from './lib/editor/constants'
@@ -27,28 +27,43 @@ import { renderFretBoard } from './lib/renderer/fretboard/fretboard'
2727import './lib/renderer/lightbox'
2828import { renderCSVPreview } from './lib/renderer/csvpreview'
2929
30- import { escapeAttrValue } from './render'
30+ import * as renderUtils from './render' // Import namespace
3131import { sanitizeUrl } from './utils'
3232
3333import markdownit from 'markdown-it'
3434import markdownitContainer from 'markdown-it-container'
35+ import markdownitAbbr from 'markdown-it-abbr'
36+ import markdownitFootnote from 'markdown-it-footnote'
37+ import markdownitDeflist from 'markdown-it-deflist'
38+ import markdownitMark from 'markdown-it-mark'
39+ import markdownitIns from 'markdown-it-ins'
40+ import markdownitSub from 'markdown-it-sub'
41+ import markdownitSup from 'markdown-it-sup'
42+ import markdownitMathjax from 'markdown-it-mathjax'
43+ import markdownitRuby from 'markdown-it-ruby'
44+
45+ import '@hackmd/emojify.js'
3546
3647/* Defined regex markdown it plugins */
3748import Plugin from 'markdown-it-regexp'
3849
39- require ( 'prismjs/themes/prism.css' )
40- require ( 'prismjs/components/prism-wiki' )
41- require ( 'prismjs/components/prism-haskell' )
42- require ( 'prismjs/components/prism-go' )
43- require ( 'prismjs/components/prism-typescript' )
44- require ( 'prismjs/components/prism-jsx' )
45- require ( 'prismjs/components/prism-makefile' )
46- require ( 'prismjs/components/prism-gherkin' )
50+ import plantumlEncoder from 'plantuml-encoder'
4751
48- require ( './lib/common/login' )
49- require ( '../vendor/md-toc' )
50- let viz = new window . Viz ( )
51- const plantumlEncoder = require ( 'plantuml-encoder' )
52+ import 'prismjs/themes/prism.css'
53+ import 'prismjs/components/prism-wiki'
54+ import 'prismjs/components/prism-haskell'
55+ import 'prismjs/components/prism-go'
56+ import 'prismjs/components/prism-typescript'
57+ import 'prismjs/components/prism-jsx'
58+ import 'prismjs/components/prism-makefile'
59+ import 'prismjs/components/prism-gherkin'
60+
61+ import './lib/common/login'
62+ import '../vendor/md-toc'
63+ import { instance as vizInstance } from '@viz-js/viz'
64+
65+ let viz = null
66+ vizInstance ( ) . then ( instance => { viz = instance } )
5267
5368const ui = getUIElements ( )
5469
@@ -186,7 +201,7 @@ export function renderTags (view) {
186201
187202function slugifyWithUTF8 ( text ) {
188203 // remove HTML tags and trim spaces
189- let newText = stripTags ( text . toString ( ) . trim ( ) )
204+ let newText = stringUtils . stripTags ( text . toString ( ) . trim ( ) ) // Use namespace
190205 // replace space between words with dashes
191206 newText = newText . replace ( / \s + / g, '-' )
192207 // slugify string to make it valid as an attribute
@@ -345,15 +360,15 @@ export function finishView (view) {
345360 if ( $ ( value ) . children ( ) . length === 0 ) {
346361 // strip HTML tags to avoid stored XSS
347362 const gistid = value . getAttribute ( 'data-gist-id' )
348- value . setAttribute ( 'data-gist-id' , stripTags ( gistid ) )
363+ value . setAttribute ( 'data-gist-id' , stringUtils . stripTags ( gistid ) ) // Use namespace
349364 const gistfile = value . getAttribute ( 'data-gist-file' )
350- if ( gistfile ) value . setAttribute ( 'data-gist-file' , stripTags ( gistfile ) )
365+ if ( gistfile ) value . setAttribute ( 'data-gist-file' , stringUtils . stripTags ( gistfile ) ) // Use namespace
351366 const gistline = value . getAttribute ( 'data-gist-line' )
352- if ( gistline ) value . setAttribute ( 'data-gist-line' , stripTags ( gistline ) )
367+ if ( gistline ) value . setAttribute ( 'data-gist-line' , stringUtils . stripTags ( gistline ) ) // Use namespace
353368 const gisthighlightline = value . getAttribute ( 'data-gist-highlight-line' )
354- if ( gisthighlightline ) value . setAttribute ( 'data-gist-highlight-line' , stripTags ( gisthighlightline ) )
369+ if ( gisthighlightline ) value . setAttribute ( 'data-gist-highlight-line' , stringUtils . stripTags ( gisthighlightline ) ) // Use namespace
355370 const gistshowloading = value . getAttribute ( 'data-gist-show-loading' )
356- if ( gistshowloading ) value . setAttribute ( 'data-gist-show-loading' , stripTags ( gistshowloading ) )
371+ if ( gistshowloading ) value . setAttribute ( 'data-gist-show-loading' , stringUtils . stripTags ( gistshowloading ) ) // Use namespace
357372 $ ( value ) . gist ( window . viewAjaxCallback )
358373 }
359374 } )
@@ -406,27 +421,27 @@ export function finishView (view) {
406421 } )
407422 // graphviz
408423 var graphvizs = view . find ( 'div.graphviz.raw' ) . removeClass ( 'raw' )
409- graphvizs . each ( function ( key , value ) {
424+ graphvizs . each ( async function ( key , value ) {
410425 try {
411426 var $value = $ ( value )
412427 const options = deserializeParamAttributeFromElement ( value )
413428 var $ele = $ ( value ) . parent ( ) . parent ( )
414429 $value . unwrap ( )
415- viz . renderString ( $value . text ( ) , options )
430+ viz . viz . renderSVGElement ( $value . text ( ) , options )
416431 . then ( graphviz => {
417432 if ( ! graphviz ) throw Error ( 'viz.js output empty graph' )
418433 $value . html ( graphviz )
419434
420435 $ele . addClass ( 'graphviz' )
421436 $value . children ( ) . unwrap ( )
422437 } )
423- . catch ( err => {
424- viz = new window . Viz ( )
438+ . catch ( async err => {
439+ viz = await vizInstance ( )
425440 $value . parent ( ) . append ( `<div class="alert alert-warning">${ escapeHTML ( err ) } </div>` )
426441 console . warn ( err )
427442 } )
428443 } catch ( err ) {
429- viz = new window . Viz ( )
444+ viz = await vizInstance ( )
430445 $value . parent ( ) . append ( `<div class="alert alert-warning">${ escapeHTML ( err ) } </div>` )
431446 console . warn ( err )
432447 }
@@ -830,8 +845,8 @@ export function exportToHTML (view) {
830845 html : src [ 0 ] . outerHTML ,
831846 'ui-toc' : toc . html ( ) ,
832847 'ui-toc-affix' : tocAffix . html ( ) ,
833- lang : ( md && md . meta && md . meta . lang ) ? `lang="${ escapeAttrValue ( md . meta . lang ) } "` : null ,
834- dir : ( md && md . meta && md . meta . dir ) ? `dir="${ escapeAttrValue ( md . meta . dir ) } "` : null
848+ lang : ( md && md . meta && md . meta . lang ) ? `lang="${ renderUtils . escapeAttrValue ( md . meta . lang ) } "` : null , // Use namespace
849+ dir : ( md && md . meta && md . meta . dir ) ? `dir="${ renderUtils . escapeAttrValue ( md . meta . dir ) } "` : null // Use namespace
835850 }
836851 const html = template ( context )
837852 // console.log(html);
@@ -1192,23 +1207,22 @@ export const md = markdownit('default', {
11921207} )
11931208window . md = md
11941209
1195- md . use ( require ( 'markdown-it-abbr' ) )
1196- md . use ( require ( 'markdown-it-footnote' ) )
1197- md . use ( require ( 'markdown-it-deflist' ) )
1198- md . use ( require ( 'markdown-it-mark' ) )
1199- md . use ( require ( 'markdown-it-ins' ) )
1200- md . use ( require ( 'markdown-it-sub' ) )
1201- md . use ( require ( 'markdown-it-sup' ) )
1202- md . use ( require ( 'markdown-it-mathjax' ) ( {
1210+ md . use ( markdownitAbbr )
1211+ md . use ( markdownitFootnote )
1212+ md . use ( markdownitDeflist )
1213+ md . use ( markdownitMark )
1214+ md . use ( markdownitIns )
1215+ md . use ( markdownitSub )
1216+ md . use ( markdownitSup )
1217+ md . use ( markdownitMathjax ( {
12031218 beforeMath : '<span class="mathjax raw">' ,
12041219 afterMath : '</span>' ,
12051220 beforeInlineMath : '<span class="mathjax raw">\\(' ,
12061221 afterInlineMath : '\\)</span>' ,
12071222 beforeDisplayMath : '<span class="mathjax raw">\\[' ,
12081223 afterDisplayMath : '\\]</span>'
12091224} ) )
1210- md . use ( require ( 'markdown-it-imsize' ) )
1211- md . use ( require ( 'markdown-it-ruby' ) )
1225+ md . use ( markdownitRuby )
12121226
12131227window . emojify . setConfig ( {
12141228 blacklist : {
@@ -1421,11 +1435,17 @@ const pdfPlugin = new Plugin(
14211435 }
14221436)
14231437
1438+ // Helper function to escape characters with special meaning in RegExp
1439+ function escapeRegExp ( string ) {
1440+ // $& means the whole matched string
1441+ return string . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' )
1442+ }
1443+
14241444const emojijsPlugin = new Plugin (
14251445 // regexp to match emoji shortcodes :something:
14261446 // We generate an universal regex that guaranteed only contains the
14271447 // emojies we have available. This should prevent all false-positives
1428- new RegExp ( ':(' + window . emojify . emojiNames . map ( ( item ) => { return RegExp . escape ( item ) } ) . join ( '|' ) + '):' , 'i' ) ,
1448+ new RegExp ( ':(' + window . emojify . emojiNames . map ( ( item ) => { return escapeRegExp ( item ) } ) . join ( '|' ) + '):' , 'i' ) ,
14291449
14301450 ( match , utils ) => {
14311451 const emoji = match [ 1 ] . toLowerCase ( )
0 commit comments