vitepress中实现在线编写

最近又在倒腾vitepress,想着能不能在线编写文档,然后直接上传,然后就有了这篇文档(这就是用在线编写功能写的)。

顺便把之前的网站精简一下,因为有些方法,官方已经提供了,就不用自己写了,然后又新加了一些功能。

初始化项目的步奏看看官网,重点说一下如何实现在线编写的,该功能只是我自己的一个想法,如果大佬们有更好的方法,欢迎提供一下。

MarkDown编辑器

编辑器我选用的是md-editor-v3,使用简单,功能完善。

安装

javascript 复制代码
pnpm i md-editor-v3

用法

vue 复制代码
<template>
  <MdEditor v-model="text" />
</template>

<script setup>
import { ref } from 'vue';
import { MdEditor } from 'md-editor-v3';
import 'md-editor-v3/lib/style.css';

const text = ref('# Hello Editor');
</script>

vitepress中使用

vitepress是可以直接在md文件中写vue3的,所以这里可以直接新建一个md文件,然后引入组件。

vue 复制代码
// editor.md
<script setup>
import editor from '../.vitepress/components/editor.vue'
</script>
<editor />
vue 复制代码
<!-- editor.vue -->
<script setup>
import { ref, computed } from 'vue';
import { MdEditor } from 'md-editor-v3';
import 'md-editor-v3/lib/style.css';
import { useData } from 'vitepress';

const content = ref('# Hello Editor');
const theme = computed(()=>useData().isDark.value ? 'dark' : 'light')

const handleSave = () => {
  // 请求保存接口
  ...
}
</script>
<template>
  <MdEditor v-model="content" :theme="theme"/>
  <button class="save-btn" @click="handleSave">保存</button>
</template>
<style>
.save-btn{
  float: right;
  margin-top: 10px;
}
</style>

保存

说一下我的整个想法,我是在前端请求保存以后,后端(nodejs)直接把传过来的数据保存到vitepress对应的文件夹下,整个的数据格式就是一个markdown形式的字符串,然后切换到vitepress目录下运行npm run build。

关键代码

获取数据,拼接处理

javascript 复制代码
  // 前端传过来的 text: 文档标题,link:文件名(vitepress用于跳转),content:文档内容
  const params = ctx.request.body
  const dates = dayjs(new Date).format('YYYY-MM-DD')
  const fileName = `${params.link}.md`
  // vitepress对应的文件夹
  const toFilePath = path.resolve(__dirname, '../../../Woung/docs/blog');
  // 需要保存的当前文件路径
  const filePath = path.join(toFilePath, fileName)
  let msg = ''
  // 拼接文档frontmatter
  let markdown = `---
title: ${params.text}
author: Younglina
date:  ${dates}
categories:
- 文档
tags:
- 记录
---
${params.content}
`

保存数据,重新打包

javascript 复制代码
fs.writeFile(filePath, markdown, 'utf-8', (err) => {
  if (err) {
    ctx.body = {
      "code": 200,
      "message": err
    }
  }

  // 处理vitepress的sidebar,我的vitepress是把sidebar抽离出了一个json文件
  const sidebarPath = path.resolve(__dirname, '../../../Woung/docs/.vitepress/sidebar.json');
  // 读取文件内容
  fs.readFile(sidebarPath, 'utf8', (err, data) => {
    if (err) {
      console.error('Error reading file:', err);
      return;
    }

    const sidebar = JSON.parse(data)
    const blogs = sidebar['/blog/'].items[0].items
    // 把当前文件信息存储到sidebar中,并覆盖原文件
    blogs.push({ text: params.text, link: params.link })
    fs.writeFile(sidebarPath, JSON.stringify(sidebar), 'utf-8', (err) => {
      if (err) {
        console.error(err)
      }
    })
  });

  // 最后使用nodejs的child_process,切换到vitepress根目录,执行build
  const buildPath = path.resolve(__dirname, '../../../Woung');
  const command = `cd ${buildPath} && npm run build`;

  exec(command, (error, stdout, stderr) => {
    if (error) {
      console.error('Error executing command:', error);
      return;
    }

    console.log('Command executed successfully:', stdout);
  });
})

效果

觉得编写的地方小,编辑器有全屏的功能

相关推荐
啾咪orJIUMI1 小时前
Vue 3 + 高德地图封装实战:点位、区域、Loca 流光与 Vue 信息窗体
前端·vue.js
FRing031 小时前
vue2干货总结8(路由模块封装、router-link、声明式导航、路由重定向、编程式导航)
开发语言·前端·vue.js·vue2
无懈可击2 小时前
Vue 可视化AI表单设计器 FcDesigner v3.6 版本发布,支持导出 SQL功能!
vue.js·开源·ai编程
用户298698530142 小时前
Python 实战:将 HTML 批量转换为 Markdown
python·html·markdown
徐小夕2 小时前
JitArchives,一款基于JitWord协同AI文档打造的企业级电子档案系统
前端·vue.js·github
头发还在的女程序员5 小时前
基于 Spring Boot + Vue 3 + uni-app 的灵活用工系统:四端架构与业务流程设计
vue.js·spring boot·uni-app·灵活用工·灵工系统
FRing031 天前
vue2干货总结7(自定义指令、插槽、单页应用程序、路由的使用(VueRouter))
开发语言·前端·vue.js·vue2
ruanno_top1 天前
点全选,已选的被清空了:Transfer 三态复选框的三个 bug
vue.js
时间的情敌1 天前
Cesium 加载 3D Tiles 倾斜摄影、压平、样式设置(Vue3 实战)
vue.js·3d·cesium
喵喵匠2 天前
一份 Markdown 发十个平台:cspub 一稿多投
自动化·markdown·效率工具·仓颉·cli工具