more markdown helps

This commit is contained in:
Your Name 2023-06-03 13:47:45 -04:00 committed by Henri Dickson
parent f379fc3a7d
commit ba24a22f77
3 changed files with 66 additions and 14 deletions

View file

@ -20,13 +20,18 @@ _mistune_plugins = [
_markdown = mistune.create_markdown(plugins=_mistune_plugins)
def convert_leading_space_in_md(body) -> str:
body = re.sub(r"^\s+$", "", body, flags=re.MULTILINE)
body = re.sub(
r"^(\u2003*)( +)",
lambda s: "\u2003" * ((len(s[2]) + 1) // 2 + len(s[1])),
body,
flags=re.MULTILINE,
)
return body
def render_md(s) -> str:
# s = "\n".join(
# [
# re.sub(r"^(\u2003+)", lambda s: " " * len(s[0]), line)
# for line in s.split("\n")
# ]
# )
return cast(str, _markdown(s))

View file

@ -31,9 +31,9 @@
<hr>
<div class="grid">
<div>
<label for="leading_space" style="display: inline;">
<label for="leading_space" style="display: inline">
<input type="checkbox" name="leading_space" value="1" id="leading_space">
转换行首空格为全角
保存时<span data-tooltip="希望行首缩进但不便输全角时可选此替换两半角为一全角">将行首空格替换为全角</span>
</label>
</div>
<div>{{ form.visibility }}</div>
@ -64,6 +64,56 @@
</script>
{{ form.media }}
</form>
<hr>
<details>
<summary>Markdown语法参考</summary>
<pre>
标题
====
副标题
------
  新起段落需隔开一空行
  段首缩进需使用中文全角空格
[链接](https://zh.wikipedia.org/wiki/Markdown)
**粗体** *斜体* ==高亮== ~~删除~~
^上^标 ~下~标 [拼(pīn)音(yīn)]
> 引用
> 文本
>> 多级引用
行内 >! 剧透预警 !< (在短评中也可使用)
>! 多行
>! 剧透
引用外部图片 ![](https://upload.wikimedia.org/wikipedia/en/8/80/Wikipedia-logo-v2.svg)
上传图片可拖拽图片文件到编辑框,或从剪贴板粘贴
---
- Bullet
- Points
content in paragraph with footnote[^1] markup.
[^1]: footnote explain
```
code
```
Table Header | Second Header
------------- | -------------
Content Cell | Content Cell
Content Cell | Content Cell
</pre>
</details>
</article>
</div>
<aside>

View file

@ -9,6 +9,8 @@ from django.db.models import Count
from django.utils import timezone
from django.utils.dateparse import parse_datetime
from django.core.paginator import Paginator
from journal.renderers import convert_leading_space_in_md
from .models import *
from django.conf import settings
from django.http import HttpResponseRedirect
@ -570,12 +572,7 @@ def review_edit(request, item_uuid, review_uuid=None):
)
body = form.instance.body
if request.POST.get("leading_space"):
body = re.sub(
r"^(\u2003*)( +)",
lambda s: "\u2003" * ((len(s[2]) + 1) // 2 + len(s[1])),
body,
flags=re.MULTILINE,
)
body = convert_leading_space_in_md(body)
review = Review.review_item_by_user(
item,
request.user,