view app/assets/javascripts/application.coffee @ 464:f11862e58af4

Canonicalize 0-prefixed ids as well
author nanaya <me@nanaya.pro>
date Mon, 10 Dec 2018 02:38:45 +0900
parents 1fc5ddcde386
children
line wrap: on
line source

#= require jquery
#= require bootstrap/transition
#= require bootstrap/collapse
#= require bootstrap/modal
#= require bootstrap/tab
#= require highlightjs
#= require commonmark

$(document).on "click", ".js-paste-clear", (e) ->
  e.preventDefault()
  $(".js-paste-paste")
    .val("")
    .focus()


loadLanguageItems = ->
  $languageSelectBox = $("#paste_language")
  return unless $languageSelectBox.length

  for language in hljs.listLanguages().sort()
    $languageSelectBox.append $("<option />", value: language, text: language)


markdownfy = (plaintext) ->
  reader = new commonmark.Parser
  writer = new commonmark.HtmlRenderer(safe: true)
  writer.render reader.parse(plaintext)


pasteText = ->
  $(".js-paste-paste").val() || $(".js-paste-pl").text()


highlightText = ->
  $hlBox = $(".js-paste-hl")
  return if $hlBox.attr("data-processed") == "1"

  $hlBox.text pasteText()

  highlight = -> hljs.highlightBlock($hlBox[0])
  setTimeout highlight, 0

  $hlBox.attr "data-processed", "1"


markdownText = ->
  $mdBox = $(".js-paste-md")
  return if $mdBox.attr("data-processed") == "1"

  $mdBox.html markdownfy(pasteText())
  $mdBox.find("a").attr("rel", "nofollow")
  $mdBox.attr "data-processed", "1"


setHash = (e) ->
  return unless history.replaceState

  newLocation = "##{e.currentTarget.getAttribute("data-mode")}"
  if newLocation == "#pl"
    newLocation = window.location.pathname

  history.replaceState null, "", newLocation


showPreview = (e) ->
  e.preventDefault()
  return if pasteText() == ""
  $(".js-paste-preview-md-box").html markdownfy(pasteText())
  $(".js-paste-preview-md-modal").modal "show"


switchToCurrentHash = ->
  return unless $(".js-showing-paste").length

  format = window.location.hash.slice(1)
  $(".js-show-tab[data-mode=#{format}]").click()


$ loadLanguageItems
$ switchToCurrentHash
$(document).on "click", ".js-paste-preview-md", showPreview
$(document).on "click", ".js-show-tab[data-mode=hl]", highlightText
$(document).on "click", ".js-show-tab[data-mode=md]", markdownText
$(document).on "click", ".js-show-tab", setHash