-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from lgc2333/main
- Loading branch information
Showing
6 changed files
with
130 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
.markdown-body, | ||
.text { | ||
width: fit-content !important; | ||
min-width: 0px !important; | ||
margin: 0px !important; | ||
padding: 15px !important; | ||
} | ||
|
||
.markdown-body code, | ||
.markdown-body kbd, | ||
.markdown-body pre, | ||
.markdown-body samp { | ||
font-family: 'JetBrains Mono', 'Cascadia Mono', 'Segoe UI Mono', | ||
'Liberation Mono', 'Menlo', 'Monaco', 'Consolas', 'Roboto Mono', | ||
'Courier New', 'Courier', 'Microsoft YaHei UI', monospace !important; | ||
} | ||
|
||
.markdown-body code { | ||
white-space: pre-wrap !important; | ||
word-wrap: break-word !important; | ||
} | ||
|
||
.markdown-body > *:last-child > pre { | ||
margin-bottom: 0px !important; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from pathlib import Path | ||
|
||
import markdown | ||
from nonebot import logger, require | ||
|
||
from .config import config | ||
|
||
require("nonebot_plugin_htmlrender") | ||
from nonebot_plugin_htmlrender.data_source import ( # noqa: E402 | ||
TEMPLATES_PATH, | ||
env, | ||
get_new_page, | ||
read_tpl, | ||
) | ||
|
||
ADDITIONAL_CSS = (Path(__file__).parent / "res" / "additional.css").read_text( | ||
encoding="u8" | ||
) | ||
|
||
|
||
async def text_to_img(text: str) -> bytes: | ||
template = env.get_template("text.html") | ||
content = await template.render_async( | ||
text=text, | ||
css="\n".join([await read_tpl("text.css"), ADDITIONAL_CSS]), | ||
) | ||
|
||
async with get_new_page( | ||
viewport={"width": config.IMG_MAX_WIDTH, "height": 1000} | ||
) as page: | ||
await page.goto(f"file://{TEMPLATES_PATH}") | ||
await page.set_content(content, wait_until="networkidle") | ||
|
||
text_element = await page.query_selector(".text") | ||
assert text_element | ||
|
||
return await text_element.screenshot(type="png") | ||
|
||
|
||
# 个人微调 | ||
async def md_to_img(md: str) -> bytes: | ||
logger.debug(md) | ||
|
||
md = markdown.markdown( | ||
md, | ||
extensions=[ | ||
"pymdownx.tasklist", | ||
"tables", | ||
"fenced_code", | ||
"codehilite", | ||
"mdx_math", | ||
"pymdownx.tilde", | ||
], | ||
extension_configs={"mdx_math": {"enable_dollar_delimiter": True}}, | ||
) | ||
logger.debug(md) | ||
|
||
extra = "" | ||
if "math/tex" in md: | ||
katex_css = await read_tpl("katex/katex.min.b64_fonts.css") | ||
katex_js = await read_tpl("katex/katex.min.js") | ||
mathtex_js = await read_tpl("katex/mathtex-script-type.min.js") | ||
extra = ( | ||
f'<style type="text/css">{katex_css}</style>' | ||
f"<script defer>{katex_js}</script>" | ||
f"<script defer>{mathtex_js}</script>" | ||
) | ||
|
||
css = "\n".join( | ||
[ | ||
await read_tpl("github-markdown-light.css"), | ||
await read_tpl("pygments-default.css"), | ||
ADDITIONAL_CSS, | ||
], | ||
) | ||
template = env.get_template("markdown.html") | ||
content = await template.render_async(md=md, css=css, extra=extra) | ||
|
||
async with get_new_page( | ||
viewport={"width": config.IMG_MAX_WIDTH, "height": 1000} | ||
) as page: | ||
await page.goto(f"file://{TEMPLATES_PATH}") | ||
await page.set_content(content, wait_until="networkidle") | ||
|
||
article = await page.query_selector("article") | ||
assert article | ||
|
||
return await article.screenshot(type="png") |