comparison app/assets/javascripts/application.coffee @ 402:a18e19139f57

Markdown preview \o/
author nanaya <me@myconan.net>
date Sun, 06 Sep 2015 04:20:50 +0900
parents 851432f21603
children 3d434e15ddb1
comparison
equal deleted inserted replaced
401:6bb805283daf 402:a18e19139f57
9 .val("") 9 .val("")
10 .focus() 10 .focus()
11 11
12 12
13 pasteText = -> 13 pasteText = ->
14 $(".js-paste-pl").text() 14 $(".js-paste-paste").val() || $(".js-paste-pl").text()
15 15
16 16
17 setHash = (newLocation = window.location.pathname) -> 17 setHash = (newLocation = window.location.pathname) ->
18 return unless history.replaceState 18 return unless history.replaceState
19 19
20 newLocation ||= window.location.pathname 20 newLocation ||= window.location.pathname
21 history.replaceState null, "", newLocation 21 history.replaceState null, "", newLocation
22
23
24 markdownfy = (plaintext) ->
25 reader = new commonmark.Parser
26 writer = new commonmark.HtmlRenderer(safe: true)
27 writer.render reader.parse(plaintext)
22 28
23 29
24 $(document).on "click", ".js-show-tab-pl", -> 30 $(document).on "click", ".js-show-tab-pl", ->
25 setHash() 31 setHash()
26 32
41 $(document).on "click", ".js-show-tab-md", -> 47 $(document).on "click", ".js-show-tab-md", ->
42 setHash "#md" 48 setHash "#md"
43 $mdBox = $(".js-paste-md") 49 $mdBox = $(".js-paste-md")
44 return if $mdBox.data("processed") 50 return if $mdBox.data("processed")
45 51
46 reader = new commonmark.Parser 52 $mdBox.html markdownfy(pasteText())
47 writer = new commonmark.HtmlRenderer(safe: true)
48 parsed = reader.parse pasteText()
49
50 $mdBox.html writer.render(parsed)
51 $mdBox.find("a").attr("rel", "nofollow") 53 $mdBox.find("a").attr("rel", "nofollow")
52 $mdBox.data "processed", true 54 $mdBox.data "processed", true
55
56
57 $(document).on "click", ".js-paste-preview-md", ->
58 return if pasteText() == ""
59 $(".js-paste-preview-md-box").html markdownfy(pasteText())
60 $(".js-paste-preview-md-modal").modal "show"
61
53 62
54 $(document).on "ready", -> 63 $(document).on "ready", ->
55 return if $(".js-showing-paste").length < 1 64 return if $(".js-showing-paste").length < 1
56 65
57 format = window.location.hash.slice(1) 66 format = window.location.hash.slice(1)