git.s-ol.nu mmm / da4d528
fix syntax highlighting in markdown s-ol 2 years ago
3 changed file(s) with 54 addition(s) and 28 deletion(s). Raw diff Collapse all Expand all
11 import languages from require 'mmm.highlighting'
22
33 class Editor
4 o = do
4 o = if MODE == 'CLIENT'
55 mkobj = window\eval "(function () { return {}; })"
66 (tbl) ->
77 with obj = mkobj!
22 assert success, "couldn't require 'discount'"
33
44 (md) ->
5 res = assert discount.compile md, 'githubtags'
5 res = assert discount.compile md, 'githubtags', 'fencedcode'
66 res.body
77 else
88 assert window and window.marked, "marked.js not found"
9
10 o = do
11 mkobj = window\eval "(function () { return {}; })"
12 (tbl) ->
13 with obj = mkobj!
14 for k,v in pairs(tbl)
15 obj[k] = v
16
17 trim = (str) -> str\match '^ *(..-) *$'
18
19 window.marked\setOptions o {
20 gfm: true
21 smartypants: true
22 langPrefix: 'lang-'
23 highlight: (code, lang) =>
24 code = trim code
25 result = if lang and #lang > 0
26 window.hljs\highlight lang, code, true
27 else
28 window.hljs\highlightAuto code
29
30 result.value
31 }
32
933 window\marked
1034
1135 assert markdown, "no markdown implementation found"
1818
1919 and the main part of the code, the `Editor` widget:
2020
21 class Editor
22 new: (value, mode, @fileder, @key) =>
23 @node = div class: 'editor'
24 -- 'o' is a little helper for converting a Lua table to a JS object
25 @cm = window\CodeMirror @node, o {
26 :value
27 :mode
28 lineNumber: true
29 lineWrapping: true
30 autoRefresh: true
31 theme: 'hybrid'
32 }
21 ```moonscript
22 class Editor
23 new: (value, mode, @fileder, @key) =>
24 @node = div class: 'editor'
25 -- 'o' is a little helper for converting a Lua table to a JS object
26 @cm = window\CodeMirror @node, o {
27 :value
28 :mode
29 lineNumber: true
30 lineWrapping: true
31 autoRefresh: true
32 theme: 'hybrid'
33 }
3334
34 @cm\on 'changes', (_, mirr) ->
35 window\clearTimeout @timeout if @timeout
36 @timeout = window\setTimeout (-> @change!), 300
35 @cm\on 'changes', (_, mirr) ->
36 window\clearTimeout @timeout if @timeout
37 @timeout = window\setTimeout (-> @change!), 300
3738
38 change: =>
39 @timeout = nil
40 doc = @cm\getDoc!
41 if @lastState and doc\isClean @lastState
42 -- no changes since last event
43 return
44
45 @lastState = doc\changeGeneration true
46 value = doc\getValue!
39 change: =>
40 @timeout = nil
41 doc = @cm\getDoc!
42 if @lastState and doc\isClean @lastState
43 -- no changes since last event
44 return
45
46 @lastState = doc\changeGeneration true
47 value = doc\getValue!
4748
48 @fileder.facets[@key] = value
49 BROWSER\refresh!
49 @fileder.facets[@key] = value
50 BROWSER\refresh!
51 ```
5052
5153 I chose the [CodeMirror][codemirror] library as the basis for the editor,
5254 because it seemed like one of the leanest ones I could find