markdown.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import os
  2. import re
  3. from functools import lru_cache
  4. from typing import List
  5. import markdown2
  6. from pygments.formatters import HtmlFormatter
  7. from .mermaid import Mermaid
  8. from .mixins.content_element import ContentElement
  9. class Markdown(ContentElement, component='markdown.js'):
  10. def __init__(self, content: str = '', *, extras: List[str] = ['fenced-code-blocks', 'tables']) -> None:
  11. """Markdown Element
  12. Renders Markdown onto the page.
  13. :param content: the Markdown content to be displayed
  14. :param extras: list of `markdown2 extensions <https://github.com/trentm/python-markdown2/wiki/Extras#implemented-extras>`_ (default: `['fenced-code-blocks', 'tables']`)
  15. """
  16. self.extras = extras
  17. super().__init__(content=content)
  18. self._classes = ['nicegui-markdown']
  19. self._props['codehilite_css'] = (
  20. HtmlFormatter(nobackground=True).get_style_defs('.codehilite') +
  21. HtmlFormatter(nobackground=True, style='github-dark').get_style_defs('.body--dark .codehilite')
  22. )
  23. if 'mermaid' in extras:
  24. self._props['use_mermaid'] = True
  25. self.libraries.append(Mermaid.exposed_libraries[0])
  26. def on_content_change(self, content: str) -> None:
  27. html = prepare_content(content, extras=' '.join(self.extras))
  28. if self._props.get('innerHTML') != html:
  29. self._props['innerHTML'] = html
  30. self.run_method('update', html)
  31. @lru_cache(maxsize=int(os.environ.get('MARKDOWN_CONTENT_CACHE_SIZE', '1000')))
  32. def prepare_content(content: str, extras: str) -> str:
  33. html = markdown2.markdown(remove_indentation(content), extras=extras.split())
  34. return apply_tailwind(html) # we need explicit Markdown styling because tailwind CSS removes all default styles
  35. def apply_tailwind(html: str) -> str:
  36. rep = {
  37. '<h1': '<h1 class="text-5xl mb-4 mt-6"',
  38. '<h2': '<h2 class="text-4xl mb-3 mt-5"',
  39. '<h3': '<h3 class="text-3xl mb-2 mt-4"',
  40. '<h4': '<h4 class="text-2xl mb-1 mt-3"',
  41. '<h5': '<h5 class="text-1xl mb-0.5 mt-2"',
  42. '<a': '<a class="underline text-blue-600 hover:text-blue-800 visited:text-purple-600"',
  43. '<ul': '<ul class="list-disc ml-6"',
  44. '<p>': '<p class="mb-2">',
  45. r'<div\ class="codehilite">': '<div class="codehilite mb-2 p-2">',
  46. '<code': '<code style="background-color: transparent"',
  47. }
  48. pattern = re.compile('|'.join(rep.keys()))
  49. return pattern.sub(lambda m: rep[re.escape(m.group(0))], html)
  50. def remove_indentation(text: str) -> str:
  51. """Remove indentation from a multi-line string based on the indentation of the first non-empty line."""
  52. lines = text.splitlines()
  53. while lines and not lines[0].strip():
  54. lines.pop(0)
  55. if not lines:
  56. return ''
  57. indentation = len(lines[0]) - len(lines[0].lstrip())
  58. return '\n'.join(line[indentation:] for line in lines)