Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update: 支持 Markdown 自定义样式 #104

Merged
merged 1 commit into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions amiyabot/_assets/markdown/template-dark.html

This file was deleted.

35 changes: 29 additions & 6 deletions amiyabot/_assets/markdown/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="./style/github-markdown.css">
<link rel="stylesheet" href="./style/highlight/vs.min.css">
<title>template</title>
<style>
* {
Expand All @@ -23,14 +22,13 @@

.markdown-body {
width: max-content;
max-width: 960px;
padding: 20px;
}
</style>
</head>
<body>
<div id="template">
<div class="markdown-body" v-html="markdownBody"></div>
<div class="markdown-body" :style="{ maxWidth: data.max_width + 'px' }" v-html="markdownBody"></div>
</div>
</body>
<script src="./js/highlight.min.js"></script>
Expand All @@ -47,20 +45,45 @@
el: '#template',
computed: {
markdownBody() {
return marked.parse(this.content)
return marked.parse(this.data.content)
}
},
methods: {
init(data) {
this.$set(this, 'content', data['content'])
this.loadCSSText(data.css_style)
this.loadCSSFiles(data.is_dark)

this.$set(this, 'data', data)
this.$nextTick(() => {
hljs.highlightAll()
})
},
loadCSSText(css) {
const style = document.createElement('style');
style.innerHTML = css;
document.head.appendChild(style);
},
loadCSSFiles(isDark) {
const files = isDark ?
['./style/highlight/vs2015.min.css', './style/github-markdown-dark.css'] :
['./style/highlight/vs.min.css']

for (const file of files) {
const link = document.createElement('link');
link.rel = 'stylesheet'
link.href = file
document.head.appendChild(link);
}
}
},
data() {
return {
content: ''
data: {
content: '',
max_width: 0,
css_style: '',
is_dark: false,
}
}
},
mounted() {
Expand Down
12 changes: 9 additions & 3 deletions amiyabot/builtin/messageChain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
class ChainConfig:
max_length = argv('text-max-length', int) or 100
md_template = os.path.join(cur_file_folder, '../../_assets/markdown/template.html')
md_template_dark = os.path.join(cur_file_folder, '../../_assets/markdown/template-dark.html')


class Chain:
Expand Down Expand Up @@ -182,14 +181,21 @@ def html(
def markdown(
self,
content: str,
max_width: int = 960,
css_style: str = '',
render_time: int = DEFAULT_RENDER_TIME,
is_dark: bool = False,
):
return self.html(
ChainConfig.md_template_dark if is_dark else ChainConfig.md_template,
ChainConfig.md_template,
width=50,
height=50,
data={'content': content},
data={
'content': content,
'max_width': max_width,
'css_style': css_style,
'is_dark': is_dark,
},
render_time=render_time,
)

Expand Down
Loading