Text.coffee 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. if Page.server_info?.user_settings?.theme == "dark"
  11. return "hsl(" + (hash % 360) + ",80%,70%)"
  12. else
  13. return "hsl(" + (hash % 360) + ",30%,50%)"
  14. renderMarked: (text, options={}) ->
  15. options["gfm"] = true
  16. options["breaks"] = true
  17. if options.sanitize
  18. options["renderer"] = renderer # Dont allow images
  19. text = marked(text, options)
  20. return @fixHtmlLinks text
  21. # Convert zeronet html links to relaitve
  22. fixHtmlLinks: (text) ->
  23. if window.is_proxy
  24. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="http://zero')
  25. else
  26. return text.replace(/href="http:\/\/(127.0.0.1|localhost):43110/g, 'href="')
  27. # Convert a single link to relative
  28. fixLink: (link) ->
  29. if window.is_proxy
  30. return link.replace(/http:\/\/(127.0.0.1|localhost):43110/, 'http://zero')
  31. else
  32. return link.replace(/http:\/\/(127.0.0.1|localhost):43110/, '')
  33. toUrl: (text) =>
  34. return text.replace(/[^A-Za-z0-9]/g, "+").replace(/[+]+/g, "+").replace(/[+]+$/, "")
  35. window.is_proxy = (window.location.pathname == "/")
  36. window.renderer = new Renderer()
  37. window.Text = new Text()