From 8ed9ed6429c6a3515b8f106c6244b4c0d313fe0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E9=98=B3?= Date: Sat, 11 Jan 2025 18:30:26 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=8A=96=E9=9F=B3=E8=A7=86?= =?UTF-8?q?=E9=A2=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Create/components/Editor/markdown.scss | 7 +++ .../Create/components/Editor/plugins.tsx | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/src/pages/Create/components/Editor/markdown.scss b/src/pages/Create/components/Editor/markdown.scss index 77d438a..47a4846 100644 --- a/src/pages/Create/components/Editor/markdown.scss +++ b/src/pages/Create/components/Editor/markdown.scss @@ -168,4 +168,11 @@ position: relative; cursor: not-allowed; } + + .douyin { + min-width: 320px; + min-height: 720px; + border-radius: 10px; + margin: 10px 0; + } } \ No newline at end of file diff --git a/src/pages/Create/components/Editor/plugins.tsx b/src/pages/Create/components/Editor/plugins.tsx index 29482c8..8b100ee 100644 --- a/src/pages/Create/components/Editor/plugins.tsx +++ b/src/pages/Create/components/Editor/plugins.tsx @@ -19,9 +19,54 @@ import 'katex/dist/katex.css'; import rehypeCallouts from 'rehype-callouts'; import 'rehype-callouts/theme/obsidian'; import { remarkMark } from 'remark-mark-highlight'; +import type { Plugin } from 'unified'; +import { visit } from 'unist-util-visit'; +import type { Element, Root } from 'hast'; + +const rehypeDouyinVideo: Plugin<[], Root> = () => { + return (tree) => { + visit(tree, 'element', (node: Element) => { + if (node.tagName === 'p') { + const link = node.children[0]; + if ( + link.type === 'element' && + link.tagName === 'a' && + link.properties?.href && + typeof link.properties.href === 'string' + ) { + const match = /(?:ixigua\.com|douyin\.com)\/(\d+)/.exec(link.properties.href); + if (match) { + const videoId = match[1]; + const wrapperDiv = { + type: 'element', + tagName: 'div', + properties: { + className: 'flex justify-center' + }, + children: [{ + type: 'element', + tagName: 'iframe', + properties: { + src: `https://open.douyin.com/player/video?vid=${videoId}&autoplay=0`, + referrerPolicy: 'unsafe-url', + allowFullScreen: true, + className: 'douyin' + }, + children: [] + }] + }; + + Object.assign(node, wrapperDiv); + } + } + } + }); + }; +}; const videos = (): BytemdPlugin => { return { + rehype: (processor) => processor.use(rehypeDouyinVideo), actions: [ { title: '视频',