feat(core): move markdown parser py-markdown -> markdown-it

py-markdown was missing a lot of the common/gfm items.

ref: #14 #96 #93 #95 #90 #250 #270
This commit is contained in:
2024-09-11 19:12:05 +09:30
parent b69d210759
commit 200c9d8d8d
6 changed files with 86 additions and 6 deletions

View File

@ -1,8 +1,17 @@
import markdown as md
import re import re
from markdown_it import MarkdownIt
from mdit_py_plugins import admon, footnote, tasklists
from pygments import highlight
from pygments.formatters.html import HtmlFormatter
from pygments.lexers import get_lexer_by_name
from django.template.loader import render_to_string from django.template.loader import render_to_string
class TicketMarkdown: class TicketMarkdown:
"""Ticket and Comment markdown functions """Ticket and Comment markdown functions
@ -10,11 +19,43 @@ class TicketMarkdown:
""" """
def highlight_func(self, code: str, lang: str, _) -> str | None:
"""Use pygments for code high lighting"""
if not lang:
return None
lexer = get_lexer_by_name(lang)
formatter = HtmlFormatter(style='vs', cssclass='codehilite')
return highlight(code, lexer, formatter)
def render_markdown(self, markdown_text): def render_markdown(self, markdown_text):
"""Render Markdown
implemented using https://markdown-it-py.readthedocs.io/en/latest/index.html
Args:
markdown_text (str): Markdown text
Returns:
str: HTML text
"""
markdown_text = self.ticket_reference(markdown_text) markdown_text = self.ticket_reference(markdown_text)
return md.markdown(markdown_text, extensions=['markdown.extensions.fenced_code', 'codehilite']) md = (
MarkdownIt(
config = "commonmark",
options_update={
'highlight': self.highlight_func
}
)
return md.render(markdown_text)
def build_ticket_html(self, match): def build_ticket_html(self, match):

View File

@ -1,7 +1,7 @@
from django import template from django import template
from django.template.defaultfilters import stringfilter from django.template.defaultfilters import stringfilter
import markdown as md from core.models.ticket.markdown import TicketMarkdown
register = template.Library() register = template.Library()
@ -9,7 +9,13 @@ register = template.Library()
@register.filter() @register.filter()
@stringfilter @stringfilter
def markdown(value): def markdown(value):
return md.markdown(value, extensions=['markdown.extensions.fenced_code', 'codehilite'])
if not value:
value = None
markdown = TicketMarkdown()
return markdown.render_markdown(value)
@register.filter() @register.filter()
@stringfilter @stringfilter

View File

@ -45,6 +45,8 @@ Centurion ERP contains the following modules:
- History - History
- [Markdown](./user/core/markdown.md)
- [Multi-Tenant](./development/api/models/access_organization_permission_checking.md#permission-checking) - [Multi-Tenant](./development/api/models/access_organization_permission_checking.md#permission-checking)
- [Single Sign-On {SSO}](./user/configuration.md#single-sign-on) - [Single Sign-On {SSO}](./user/configuration.md#single-sign-on)

View File

@ -0,0 +1,26 @@
---
title: Markdown
description: Markdown Documentation as part of the Core Module for Centurion ERP by No Fuss Computing
date: 2024-06-07
template: project.html
about: https://gitlab.com/nofusscomputing/infrastructure/configuration-management/centurion_erp
---
All Text fields, that is those that are multi-lined support markdown text.
## Features
- CommonMark Markdown
- Tables
- Strikethrough
- Code highlighting
- Admonitions
- Linkify
- Task Lists

View File

@ -190,6 +190,8 @@ nav:
- projects/centurion_erp/user/core/index.md - projects/centurion_erp/user/core/index.md
- projects/centurion_erp/user/core/markdown.md
- projects/centurion_erp/user/core/tickets.md - projects/centurion_erp/user/core/tickets.md
- ITAM: - ITAM:

View File

@ -18,8 +18,11 @@ drf-spectacular[sidecar]==0.27.2
django_split_settings==1.3.1 django_split_settings==1.3.1
markdown==3.6 markdown-it-py[plugins]==3.0.0
Pygments markdown-it-py[linkify]==3.0.0
Pygments==2.18.0
celery==5.4.0 celery==5.4.0
django-celery-results==2.5.1 django-celery-results==2.5.1