This commit is contained in:
abearxiong committed 2025-11-10 15:42:44 +08:00
1 parent d759dbed6d
commit 090379b9d2
5 files changed
+82 -11

No files matched your search

+13 -9
View File
@@ -1,3 +1,5 @@
import { md2html } from "./md";
import { css } from "./md-css";
class MDPreview extends HTMLElement { class MDPreview extends HTMLElement {
constructor() { constructor() {
super(); super();
@@ -8,27 +10,24 @@ class MDPreview extends HTMLElement {
this.render(); this.render();
} }
getContent() { getContent() {
console.log('MDPreview getContent called');
console.log('Inner HTML:', this.innerHTML);
const isAstro = this.isAstro(); const isAstro = this.isAstro();
console.log('Is Astro:', isAstro);
console.log('Text Content:', this.textContent, this);
if(isAstro) { if(isAstro) {
const astro = this.querySelector('astro-slot'); const astro = this.querySelector('astro-slot');
if(astro) { if(astro) {
console.log('Found astro-slot:', astro);
return astro.innerHTML; return astro.innerHTML;
} }
} }
return this.innerHTML; return this.innerHTML;
} }
getHtml() {
const markdown = this.getContent();
return md2html(markdown);
}
isAstro() { isAstro() {
return this.hasAttribute('is-astro'); return this.hasAttribute('is-astro');
} }
render() { render() {
// console.log('MDPreview render called'); const content = this.getHtml();
const content = this.getContent();
console.log('Content to render:', content);
this.shadowRoot!.innerHTML = ` this.shadowRoot!.innerHTML = `
<style> <style>
/* Styles for markdown preview */ /* Styles for markdown preview */
@@ -36,9 +35,14 @@ class MDPreview extends HTMLElement {
display: block; display: block;
padding: 1em; padding: 1em;
font-family: Arial, sans-serif; font-family: Arial, sans-serif;
width: 100%;
box-sizing: border-box;
height: 100%;
overflow: auto;
} }
${css}
</style> </style>
<div id="preview"> <div id="preview" class="markdown-body">
<!-- Markdown content will be rendered here --> <!-- Markdown content will be rendered here -->
${content} ${content}
</div> </div>
+1
View File
@@ -0,0 +1 @@
export const css = `.markdown-body{color-scheme:light;-ms-text-size-adjust:100%;-webkit-text-size-adjust:100%;margin:0;color:#1f2328;background-color:#fff;font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","Noto Sans",Helvetica,Arial,sans-serif,"Apple Color Emoji","Segoe UI Emoji";font-size:16px;line-height:1.5;word-wrap:break-word}.markdown-body .octicon{display:inline-block;fill:currentColor;vertical-align:text-bottom}.markdown-body h1:hover .anchor .octicon-link:before,.markdown-body h2:hover .anchor .octicon-link:before,.markdown-body h3:hover .anchor .octicon-link:before,.markdown-body h4:hover .anchor .octicon-link:before,.markdown-body h5:hover .anchor .octicon-link:before,.markdown-body h6:hover .anchor .octicon-link:before{width:16px;height:16px;content:' ';display:inline-block;background-color:currentColor;-webkit-mask-image:url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg>");mask-image:url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' version='1.1' aria-hidden='true'><path fill-rule='evenodd' d='M7.775 3.275a.75.75 0 001.06 1.06l1.25-1.25a2 2 0 112.83 2.83l-2.5 2.5a2 2 0 01-2.83 0 .75.75 0 00-1.06 1.06 3.5 3.5 0 004.95 0l2.5-2.5a3.5 3.5 0 00-4.95-4.95l-1.25 1.25zm-4.69 9.64a2 2 0 010-2.83l2.5-2.5a2 2 0 012.83 0 .75.75 0 001.06-1.06 3.5 3.5 0 00-4.95 0l-2.5 2.5a3.5 3.5 0 004.95 4.95l1.25-1.25a.75.75 0 00-1.06-1.06l-1.25 1.25a2 2 0 01-2.83 0z'></path></svg>")}.markdown-body details,.markdown-body figcaption,.markdown-body figure{display:block}.markdown-body summary{display:list-item}.markdown-body [hidden]{display:none!important}.markdown-body a{background-color:transparent;color:#0969da;text-decoration:none}.markdown-body abbr[title]{border-bottom:none;-webkit-text-decoration:underline dotted;text-decoration:underline dotted}.markdown-body b,.markdown-body strong{font-weight:600}.markdown-body dfn{font-style:italic}.markdown-body h1{margin:.67em 0;font-weight:600;padding-bottom:.3em;font-size:2em;border-bottom:1px solid #d1d9e0b3}.markdown-body mark{background-color:#fff8c5;color:#1f2328}.markdown-body small{font-size:90%}.markdown-body sub,.markdown-body sup{font-size:75%;line-height:0;position:relative;vertical-align:baseline}.markdown-body sub{bottom:-.25em}.markdown-body sup{top:-.5em}.markdown-body img{border-style:none;max-width:100%;box-sizing:content-box}.markdown-body code,.markdown-body kbd,.markdown-body pre,.markdown-body samp{font-family:monospace;font-size:1em}.markdown-body figure{margin:1em 2.5rem}.markdown-body hr{box-sizing:content-box;overflow:hidden;background:0 0;border-bottom:1px solid #d1d9e0b3;height:.25em;padding:0;margin:1.5rem 0;background-color:#d1d9e0;border:0}.markdown-body input{font:inherit;margin:0;overflow:visible;font-family:inherit;font-size:inherit;line-height:inherit}.markdown-body [type=button],.markdown-body [type=reset],.markdown-body [type=submit]{-webkit-appearance:button;appearance:button}.markdown-body [type=checkbox],.markdown-body [type=radio]{box-sizing:border-box;padding:0}.markdown-body [type=number]::-webkit-inner-spin-button,.markdown-body [type=number]::-webkit-outer-spin-button{height:auto}.markdown-body [type=search]::-webkit-search-cancel-button,.markdown-body [type=search]::-webkit-search-decoration{-webkit-appearance:none;appearance:none}.markdown-body ::-webkit-input-placeholder{color:inherit;opacity:.54}.markdown-body ::-webkit-file-upload-button{-webkit-appearance:button;appearance:button;font:inherit}.markdown-body a:hover{text-decoration:underline}.markdown-body ::placeholder{color:#59636e;opacity:1}.markdown-body hr::before{display:table;content:""}.markdown-body hr::after{display:table;clear:both;content:""}.markdown-body table{border-spacing:0;border-collapse:collapse;display:block;width:max-content;max-width:100%;overflow:auto;font-variant:tabular-nums}.markdown-body td,.markdown-body th{padding:0}.markdown-body details summary{cursor:pointer}.markdown-body [role=button]:focus,.markdown-body a:focus,.markdown-body input[type=checkbox]:focus,.markdown-body input[type=radio]:focus{outline:2px solid #0969da;outline-offset:-2px;box-shadow:none}.markdown-body [role=button]:focus:not(:focus-visible),.markdown-body a:focus:not(:focus-visible),.markdown-body input[type=checkbox]:focus:not(:focus-visible),.markdown-body input[type=radio]:focus:not(:focus-visible){outline:solid 1px transparent}.markdown-body [role=button]:focus-visible,.markdown-body a:focus-visible,.markdown-body input[type=checkbox]:focus-visible,.markdown-body input[type=radio]: Line truncated
+26
View File
@@ -0,0 +1,26 @@
import { Marked } from 'marked';
import hljs from 'highlight.js';
import { markedHighlight } from 'marked-highlight';
const markedAndHighlight = new Marked(
markedHighlight({
emptyLangClass: 'hljs',
langPrefix: 'hljs language-',
highlight(code, lang, info) {
const language = hljs.getLanguage(lang) ? lang : 'plaintext';
return hljs.highlight(code, { language }).value;
},
}),
);
export const md2html = (md: string) => {
const html = markedAndHighlight.parse(md);
return html;
};
export const clearMeta = (markdown?: string) => {
if (!markdown) return '';
// Remove YAML front matter if present
const yamlRegex = /^---\n[\s\S]*?\n---\n/;
return markdown.replace(yamlRegex, '');
};
+1 -1
View File
@@ -2,7 +2,7 @@ import './index.ts';
export const App = (props) => { export const App = (props) => {
return ( return (
<div style={{ display: 'contents' }}> <div className='flex justify-center justify-self-center w-[800px] '>
{/* @ts-ignore */} {/* @ts-ignore */}
<md-preview is-astro>{props.children}</md-preview> <md-preview is-astro>{props.children}</md-preview>
</div> </div>
+41 -1
View File
@@ -4,10 +4,50 @@ import { App } from '@/apps/md-preview/react.tsx';
--- ---
<Html> <Html>
<slot name="head">
<style is:global>
.markdown-body h2 {
color: red;
}
#preview h2 {
color: blue;
}
</style>
</slot>
<main> <main>
<App client:only># acb
<App client:only>
# acb
内容
## sf
- [ ] todo
## 123
- 1水电费
- 2是的
## 23
1. 12
2. 323
4. 434
---
## 哈哈
### 水电放放风
*水滚滚滚各位*
_sdf_
<div>this is div</div> <div>this is div</div>
</App> </App>
</main> </main>
</Html> </Html>