• Hexo + VSCode 插入 Markdown 图片解决办法


    最近打开 typora 时发现弹窗强更,不让用 beta 版了

    图 1

    想到自己并不是非常需要 WYSIWYG,而且也不是经常使用 typora,于是直接退回到 VSCode 了,而且在 VSCode 里可以直接打开终端操作,写完了推送到 GitHub 都很方便。
    然后就是老生常谈的图片问题,之前记录过 typora 上的 解决办法 ,VSCode 上利用扩展也可以解决,下面简单记录下。

    推荐插件

    Markdown All in One : 快捷键、生成目录、自动预览等等

    Markdown Image : 方便地在 Markdown 中插入图片,支持本地、图床或对象存储

    Pangu-Markdown : 在中英文之间加空格

    Office Viewer(Markdown Editor) : 如果有 WYSIWYG 需求的话推荐

    图片插件使用方式

    首先安装 Markdown Image 插件

    可复制图片文件或截图粘贴,快捷键 Shift + Alt + V,或右键菜单粘贴图片

    图 2

    插件基本配置

    • markdown-image.base.uploadMethod: 上传图片的方式,根据不同的方式,须设置不同的项目
    • markdown-image.base.fileNameFormat: 图片文件命名格式化字符串。支持多种变量做格式化,可同时配置文件夹格式,具体见设置

    uploadMethod 可选值为:

    图 1

    复制到本地

    uploadMethod 设置为 Local

    markdown-image.local.path: 图片本地存放路径,支持相对路径,相对于所粘贴 Markdown 文件,/ 表示打开的文件夹根目录。若路径不存在,将会自动创建

    上传到图床或 OSS

    按需选择,具体见 文档

    图 3

    自定义上传

    当你用的图床不在默认支持列表时可以编写自定义代码来上传图片,配置 markdown-image.DIY.path 为你写的代码的路径

    你的代码必须 exports 一个像 async function (filePath:string, savePath:string, markdownPath:string):string 的函数

    如:

    复制代码
    • 1
    • 2
    • 3
    • 4
    • 5
    const path = require('path'); module.exports = async function(filePath, savePath, markdownPath) { // Return a picture access link return path.relative(path.dirname(markdownPath), filePath); }
    我的自定义代码:
    复制代码
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    const { createReadStream } = require('fs') const fetch = require('node-fetch') // ^2.6.7 const FormData = require('form-data') async function upload({ filePath, preUpload, ...options }) { const form = new FormData() if (preUpload) await preUpload(filePath, form, options) const { api, fileField = 'file', formData = {}, headers = {}, isSuccess, returnUrl } = options form.append(fileField, createReadStream(filePath)) for (const [formKey, formValue] of Object.entries(formData)) { form.append(formKey, formValue) } const response = await fetch(api, { body: form, method: 'POST', headers: { ...headers, ...form.getHeaders() } }) if (!response.ok) throw new Error(response.statusText) const json = await response.json() if (isSuccess?.(json)) { return returnUrl(json) } else { throw new Error(JSON.stringify(json, null, 2)) } } // 以 bilibili 为例 module.exports = async function (filePath) { const result = await upload({ api: 'https://api.bilibili.com/动态页自行发现' filePath, fileField: 'file_up', formData: { biz: 'new_dyn', category: 'daily', csrf: '你的 CSRF Token' }, headers: { Cookie: '你的 Cookie', Origin: 'https://t.bilibili.com', Referer: 'https://t.bilibili.com/' }, isSuccess: d => d.code == 0, returnUrl: d => d.data.image_url.replace('http:', 'https:') }) return result }
    使用 upimg 上传:
    复制代码
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    const upimg = require('upimg') module.exports = async function (filePath) { // 以 bilibili 为例,文档见 npm const { url } = await upimg.bilibili.set('cookie', '你的 Cookie').upload(filePath) return url }

    Reference

    Markdown Image Readme


    fin.

  • 相关阅读:
    密度聚类:OPTICS算法详解
    Golang GMP调度模型:实现高效协程调度和执行
    C#线程间操作无效:从不是创建控件“textbox1”的线程访问它
    第三篇 部署方式--单机部署
    Cadence Allegro PCB设计88问解析(十五) 之 Allegro中如何替换过孔类型
    freeCAD不合并导入step文件
    力扣大厂热门面试算法题 - 矩阵
    固定资产可视化智能管理系统
    华为欧拉系统安装
    PrestoSQL, PrestoDB 和 Trino
  • 原文地址:https://www.cnblogs.com/himeka/p/16407020.html