Skip to content

Commit

Permalink
feat: 前后台统一Markdown组件,展示效果完全一致;解决了主题重影问题 #136
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Mar 21, 2023
1 parent 5d0a7fb commit 4c09630
Show file tree
Hide file tree
Showing 39 changed files with 1,720 additions and 1,608 deletions.
5 changes: 3 additions & 2 deletions packages/admin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@
"@bytemd/plugin-breaks": "^1.17.2",
"@bytemd/plugin-frontmatter": "^1.17.2",
"@bytemd/plugin-gfm": "^1.17.2",
"@bytemd/plugin-highlight": "^1.17.2",
"@bytemd/plugin-math": "^1.17.2",
"@bytemd/plugin-highlight-ssr": "^1.20.2",
"@bytemd/plugin-math-ssr": "^1.20.2",
"@bytemd/plugin-medium-zoom": "^1.17.2",
"@bytemd/plugin-mermaid": "^1.17.2",
"@bytemd/react": "^1.17.2",
Expand Down Expand Up @@ -93,6 +93,7 @@
"react-dom": "^17.0.0",
"react-helmet-async": "^1.2.0",
"react-monaco-editor": "^0.50.1",
"rehype-raw": "^6.1.1",
"remark-directive": "^2.0.1",
"umi": "^3.5.0",
"unist-util-visit": "^4.1.1"
Expand Down
10 changes: 5 additions & 5 deletions packages/admin/src/components/Editor/icons.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
export const icons: Record<string, string> = {
tip: `<svg viewBox="0 0 12 16" fill="currentColor" width="20" height="20">
tip: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 12 16" fill="currentColor" width="20" height="20">
<path
fill-rule="evenodd"
d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"
/>
</svg>`,
warning: `<svg viewBox="0 0 16 16" fill="currentColor" width="20" height="20">
warning: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 16 16" fill="currentColor" width="20" height="20">
<path
fill-rule="evenodd"
d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"
/>
</svg>`,
danger: `<svg viewBox="0 0 1024 1024" fill="currentColor" width="20" height="20">
danger: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 1024 1024" fill="currentColor" width="20" height="20">
<path d="M147.911 833.422V560.356a364.089 364.089 0 1 1 728.178 0v273.066H921.6v91.022H102.4v-91.022h45.511zm91.022-273.066h91.023A182.044 182.044 0 0 1 512 378.31v-91.02a273.067 273.067 0 0 0-273.067 273.067zM466.49 14.222h91.022v136.534H466.49V14.222zm399.496 127.795 64.353 64.353-96.483 96.53-64.399-64.354 96.53-96.529zM93.662 206.37l64.353-64.353 96.529 96.484-64.308 64.444-96.574-96.575z" />
</svg>`,
note: `<svg viewBox="0 0 14 16" fill="currentColor" width="20" height="20">
note: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 14 16" fill="currentColor" width="20" height="20">
<path
fill-rule="evenodd"
d="M6.3 5.69a.942.942 0 0 1-.28-.7c0-.28.09-.52.28-.7.19-.18.42-.28.7-.28.28 0 .52.09.7.28.18.19.28.42.28.7 0 .28-.09.52-.28.7a1 1 0 0 1-.7.3c-.28 0-.52-.11-.7-.3zM8 7.99c-.02-.25-.11-.48-.31-.69-.2-.19-.42-.3-.69-.31H6c-.27.02-.48.13-.69.31-.2.2-.3.44-.31.69h1v3c.02.27.11.5.31.69.2.2.42.31.69.31h1c.27 0 .48-.11.69-.31.2-.19.3-.42.31-.69H8V7.98v.01zM7 2.3c-3.14 0-5.7 2.54-5.7 5.68 0 3.14 2.56 5.7 5.7 5.7s5.7-2.55 5.7-5.7c0-3.15-2.56-5.69-5.7-5.69v.01zM7 .98c3.86 0 7 3.14 7 7s-3.14 7-7 7-7-3.12-7-7 3.14-7 7-7z"
/>
</svg>`,
info: `<svg viewBox="0 0 14 16" fill="currentColor" width="20" height="20">
info: `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 14 16" fill="currentColor" width="20" height="20">
<path
fill-rule="evenodd"
d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"
Expand Down
116 changes: 15 additions & 101 deletions packages/admin/src/components/Editor/index.less
Original file line number Diff line number Diff line change
@@ -1,104 +1,3 @@
.custom-container {
--info-title-color: #193c47;
--info-bg-color: #eef9fd;
--info-border-color: #4cb3d4;

--note-title-color: #474748;
--note-bg-color: #fdfdfe;
--note-border-color: #d4d5d8;

--tip-title-color: #003100;
--tip-bg-color: #e6f6e6;
--tip-border-color: #009400;

--warning-title-color: #4d3800;
--warning-bg-color: #fff8e6;
--warning-border-color: #e6a700;

--danger-title-color: #4b1113;
--danger-bg-color: #ffebec;
--danger-border-color: #e13238;
}
.dark .custom-container {

--info-title-color: #eef9fd;
--info-bg-color: #193c47;

--note-title-color: #fdfdfe;
--note-bg-color: #474748;

--tip-title-color: #e6f6e6;
--tip-bg-color: #003100;

--warning-title-color: #fff8e6;
--warning-bg-color: #4d3800;


--danger-title-color: #ffebec;
--danger-bg-color: #4b1113;

--detail-bg-color: #333;
--detail-text-color: #a8a8a8;
}

.custom-container.info {
background-color: var(--info-bg-color);
border-color: var(--info-border-color);
}
.custom-container.note {
background-color: var(--note-bg-color);
border-color: var(--note-border-color);
}
.custom-container.tip {
background-color: var(--tip-bg-color);
border-color: var(--tip-border-color);
}
.custom-container.warning {
background-color: var(--warning-bg-color);
border-color: var(--warning-border-color);
}
.custom-container.danger {
background-color: var(--danger-bg-color);
border-color: var(--danger-border-color);
}
.custom-container-title.info {
color: var(--info-title-color);
}
.custom-container-title.note {
color: var(--note-title-color);
}
.custom-container-title.warning {
color: var(--warning-title-color);
}
.custom-container-title.tip {
color: var(--tip-title-color);
}
.custom-container-title.danger {
color: var(--danger-title-color);
}

.custom-container {
margin: 1rem 0;
padding: 0.25rem 1rem;
border-left-width: 0.3rem;
border-left-style: solid;
border-radius: 0.5rem;
color: inherit;
}

.markdown-body .custom-container-title {
font-weight: 600;
margin: 16px 0;
display: flex;
align-items: center;
}
.markdown-body .custom-container-title svg {
margin-right: 4px;
}
body .markdown-body .custom-container .relative > pre {
margin: 0 !important;
}

.emoji-container {
position: absolute;
top: 32px;
Expand All @@ -111,3 +10,18 @@ display: none;
body .emoji-categories {
background-color: gray;
}
.relative {
position: relative;
}

.flex {
display: flex;
}

.mr-1 {
margin-right: 4px;
}

body .dark .bytemd-preview {
background: rgb(41, 43, 43);
}
19 changes: 13 additions & 6 deletions packages/admin/src/components/Editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
// import breaks from '@bytemd/plugin-breaks';
import frontmatter from '@bytemd/plugin-frontmatter';
import gfm from '@bytemd/plugin-gfm';
import highlight from '@bytemd/plugin-highlight';
import math from '@bytemd/plugin-math';
import highlight from '@bytemd/plugin-highlight-ssr';
import math from '@bytemd/plugin-math-ssr';
import mediumZoom from '@bytemd/plugin-medium-zoom';
import mermaid from '@bytemd/plugin-mermaid';
import { Editor } from '@bytemd/react';
import { Spin } from 'antd';
import 'bytemd/dist/index.css';
import 'highlight.js/styles/vs.css';
import 'katex/dist/katex.css';
import { useMemo } from 'react';
import '../../style/github-markdown.css';
import '../../style/code-light.css';
import '../../style/code-dark.css';
import '../../style/custom-container.css';
import { emoji } from './emoji';
import { imgUploadPlugin, uploadImg } from './imgUpload';
import './index.less';
import { insertMore } from './insertMore';
import { cn } from './locales';

import { useModel } from 'umi';
import { customContainer } from './customContainer';
import { customContainer } from './plugins/customContainer';
import { historyIcon } from './history';
import { rawHTML } from './raw';
import rawHTML from './rawHTML';
import { Heading } from './plugins/heading';
import { customCodeBlock } from './plugins/codeBlock';
import { LinkTarget } from './plugins/linkTarget';

export default function EditorComponent(props: {
value: string;
Expand All @@ -47,6 +51,9 @@ export default function EditorComponent(props: {
insertMore(),
rawHTML(),
historyIcon(),
Heading(),
customCodeBlock(),
LinkTarget(),
];
}, []);

Expand Down
93 changes: 93 additions & 0 deletions packages/admin/src/components/Editor/plugins/codeBlock.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import type { BytemdPlugin } from "bytemd";
import { visit } from "unist-util-visit";
import copy from 'copy-to-clipboard';
import { message } from "antd";
// FIXME: Addd Types
const codeBlockPlugin = () => (tree) => {
visit(tree, (node) => {
if (node.type === "element" && node.tagName === "pre") {
const oldChildren = JSON.parse(JSON.stringify(node.children));
const codeProperties = oldChildren.find((child: any) => child.tagName === "code").properties;
let language = "";
if (codeProperties.className) {
for (const each of codeProperties.className) {
if (each.startsWith("language-")) {
language = each.replace("language-", "");
break;
}
}
}
if (language === "mermaid") return;
// 复制按钮
const codeCopyBtn = {
type: "element",
tagName: "div",
properties: {
class: "code-copy-btn",
},
children: []
}
const languageTag = {
type: "element",
tagName: "span",
properties: {
class: "language-tag mr-1",
style: "line-height: 21px"
},
children: [
{
type: "text",
value: language
}
]
}
// 上方右侧 header
const headerRight = {
type: "element",
tagName: "div",
properties: {
class: "header-right flex",
style: "color: #6f7177",

},
children: [
languageTag, codeCopyBtn
]
}
// 包裹的 div
const wrapperDiv = {
type: "element",
tagName: "div",
properties: {
class: "code-block-wrapper relative"
},
children: [
headerRight, ...oldChildren
]
}
node.children = [wrapperDiv]
}
});
};

const onClickCopyCode = (e: PointerEvent) => {
const copyBtn = e.target as HTMLElement;
const code = copyBtn.parentElement?.parentElement?.querySelector("code")?.innerText;
copy(code);
message.success("复制成功")
}

export function customCodeBlock(): BytemdPlugin {
return {
rehype: (processor) =>
processor.use(codeBlockPlugin),
viewerEffect: ({ markdownBody }) => {
markdownBody.querySelectorAll(".code-block-wrapper").forEach((codeBlock) => {
const copyBtn = codeBlock.querySelector(".code-copy-btn")
//remove first
copyBtn.removeEventListener("click", onClickCopyCode);
copyBtn.addEventListener("click", onClickCopyCode);
})
}
};
}
Loading

0 comments on commit 4c09630

Please sign in to comment.