如何使用typora适配自定义图片上传服务

909 阅读2分钟

最近需要将飞书的文档转成md文档,便于后续的迁移到公司统一的帮助中心。进行后一步的展示。如果是直接手动进行编辑,会非常的麻烦,需要每一个文档重新排版,图片复制。 我这边默认使用的md工具是typora,编写时所见即所得,兼容性也非常好,导出的pdfdoc也非常好用。所以这里我们就需要能够在复制飞书文档时,能够自动转换飞书中的图片链接。这个时候就需要使用到typora中的图片上传功能了。

脚本

利用typora中的图片上传服务设定,调用本地的脚本直接上传图片到自定义的图片图床中。typora会替换图片地址。完成整体的转换。 image.png

步骤

:::success

  1. 获取本地图片地址或者网络图片地址
  2. 通过图床接口进行上传得到图片地址
  3. 输出对接的图片地址 :::
#!/bin/bash

result=()

function upload() {

  echo $1
  t=""
  # 判断图片是否是网络地址
  if [[ $1 == http* ]]; then
      echo @$1
      # 下载到本地
      # 随机一个名称,可以用uuid
      curday=`date +%Y%m%d%H%M%S`
      echo $curday
      curl $1 --output ${curday}.png
      t=$(curl --request POST -sL \
              -F"file=@${curday}.png" \
             --url 'https://url')
  else
      t=$(curl --request POST -sL \
        -F"file=@$1" \
        --url 'https://url')
  fi

  picAddr=$(getJsonValuesByAwk "$t" data)

  #echo $picAddr
  result[${#result[@]}]=$picAddr

}

function getJsonValuesByAwk() {
    awk -v json="$1" -v key="$2" -v defaultValue="$3" 'BEGIN{
        foundKeyCount = 0
        while (length(json) > 0) {
            # pos = index(json, "\""key"\""); ## 这行更快一些,但是如果有value是字符串,且刚好与要查找的key相同,会被误认为是key而导致值获取错误
            pos = match(json, "\""key"\"[ \\t]*?:[ \\t]*");
            if (pos == 0) {if (foundKeyCount == 0) {print defaultValue;} exit 0;}

            ++foundKeyCount;
            start = 0; stop = 0; layer = 0;
            for (i = pos + length(key) + 1; i <= length(json); ++i) {
                lastChar = substr(json, i - 1, 1)
                currChar = substr(json, i, 1)

                if (start <= 0) {
                    if (lastChar == ":") {
                        start = currChar == " " ? i + 1: i;
                        if (currChar == "{" || currChar == "[") {
                            layer = 1;
                        }
                    }
                } else {
                    if (currChar == "{" || currChar == "[") {
                        ++layer;
                    }
                    if (currChar == "}" || currChar == "]") {
                        --layer;
                    }
                    if ((currChar == "," || currChar == "}" || currChar == "]") && layer <= 0) {
                        stop = currChar == "," ? i : i + 1 + layer;
                        break;
                    }
                }
            }

            if (start <= 0 || stop <= 0 || start > length(json) || stop > length(json) || start >= stop) {
                if (foundKeyCount == 0) {print defaultValue;} exit 0;
            } else {
                print substr(json, start, stop - start);
            }

            json = substr(json, stop + 1, length(json) - stop)
        }
    }'
}

while [ $# -gt 0 ]
do
  upload "$1" $index
  shift
done
echo 'Upload Success:'
for i in ${result[*]}
do
  echo $i;
done

本地图片

直接可以通过curl进行api调用即可

网络图片

需要将图片进行下载下来,存储到本地后,再走上面的curl调用api接口的方式

curl $1 --output ${curday}.png

接口返回JSON解析

一般接口都是json的方式来返回数据,这个时候就需要从json中获取到实际的图片地址。提供了getJsonValuesByAwk方法来获取图片的地址。

脚本配置

sh E:\gitee\demo\dubbo-demo\leaf-p\src\main\resources\uploader.sh

使用shell脚本来编写具体的业务逻辑,如果是windows的用户,需要安装git bash。git安装好以后,同步将执行的bin目录配置到环境变量中,就可以执行sh命令了。 output.png

结果

可以点击验证图片上传选项 image.png 可以成功获取到图片地址。我们也可以在图片上点击右键,使用上传图片功能上传我们的图片到我们私有图床上。 image.png uploding后会生成新的图片地址。