|
@@ -3,11 +3,16 @@ from functools import lru_cache
|
|
|
from typing import List
|
|
|
|
|
|
import markdown2
|
|
|
+from fastapi.responses import PlainTextResponse
|
|
|
from pygments.formatters import HtmlFormatter # pylint: disable=no-name-in-module
|
|
|
|
|
|
+from .. import core
|
|
|
+from ..version import __version__
|
|
|
from .mermaid import Mermaid
|
|
|
from .mixins.content_element import ContentElement
|
|
|
|
|
|
+CODEHILITE_CSS_URL = f'/_nicegui/{__version__}/codehilite.css'
|
|
|
+
|
|
|
|
|
|
class Markdown(ContentElement, component='markdown.js'):
|
|
|
|
|
@@ -25,14 +30,18 @@ class Markdown(ContentElement, component='markdown.js'):
|
|
|
self.extras = extras[:]
|
|
|
super().__init__(content=content)
|
|
|
self._classes.append('nicegui-markdown')
|
|
|
- self._props['codehilite_css'] = (
|
|
|
- HtmlFormatter(nobackground=True).get_style_defs('.codehilite') +
|
|
|
- HtmlFormatter(nobackground=True, style='github-dark').get_style_defs('.body--dark .codehilite')
|
|
|
- )
|
|
|
if 'mermaid' in extras:
|
|
|
self._props['use_mermaid'] = True
|
|
|
self.libraries.append(Mermaid.exposed_libraries[0])
|
|
|
|
|
|
+ self._props['codehilite_css_url'] = CODEHILITE_CSS_URL
|
|
|
+ if not any(r for r in core.app.routes if getattr(r, 'path', None) == CODEHILITE_CSS_URL):
|
|
|
+ core.app.get(CODEHILITE_CSS_URL)(lambda: PlainTextResponse(
|
|
|
+ HtmlFormatter(nobackground=True).get_style_defs('.codehilite') +
|
|
|
+ HtmlFormatter(nobackground=True, style='github-dark').get_style_defs('.body--dark .codehilite'),
|
|
|
+ media_type='text/css',
|
|
|
+ ))
|
|
|
+
|
|
|
def _handle_content_change(self, content: str) -> None:
|
|
|
html = prepare_content(content, extras=' '.join(self.extras))
|
|
|
if self._props.get('innerHTML') != html:
|