Text.coffee 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. class Renderer extends marked.Renderer
  2. image: (href, title, text) ->
  3. return ("<code>![#{text}](#{href})</code>")
  4. class Text
  5. toColor: (text) ->
  6. hash = 0
  7. for i in [0..text.length-1]
  8. hash = text.charCodeAt(i) + ((hash << 5) - hash)
  9. color = '#'
  10. return "hsl(" + (hash % 360) + ",30%,50%)";
  11. for i in [0..2]
  12. value = (hash >> (i * 8)) & 0xFF
  13. color += ('00' + value.toString(16)).substr(-2)
  14. return color
  15. renderMarked: (text, options={}) ->
  16. options["gfm"] = true
  17. options["breaks"] = true
  18. if options.sanitize
  19. options["renderer"] = renderer # Dont allow images
  20. text = marked(text, options)
  21. return @fixHtmlLinks text
  22. # Convert zeronet html links to relaitve
  23. fixHtmlLinks: (text) ->
  24. if window.is_proxy
  25. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="http://zero')
  26. else
  27. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="')
  28. # Convert a single link to relative
  29. fixLink: (link) ->
  30. if window.is_proxy
  31. return link.replace(/http:\/\/(127.0.0.1|localhost):43110/, 'http://zero')
  32. else
  33. return link.replace(/http:\/\/(127.0.0.1|localhost):43110/, '')
  34. toUrl: (text) =>
  35. return text.replace(/[^A-Za-z0-9]/g, "+").replace(/[+]+/g, "+").replace(/[+]+$/, "")
  36. window.is_proxy = (window.location.pathname == "/")
  37. window.renderer = new Renderer()
  38. window.Text = new Text()