当前位置: 首页 > news >正文

Joplin Typora 粘贴图片 | 当使用Typora作为Joplin编辑器时,如何粘贴图片并上传到Joplin服务器,替换链接

一、背景

当我们使用Joplin时,上传图片时会自动上传到Joplin服务器并替换链接

但是Joplin的编辑器不好用,我更习惯用Typora来编辑,

然而Typora中上传的图片只能在本地,无法上传到Joplin服务器,在其他客户端也看不到图片

本文编写了一个脚本,通过Typora的上传服务器功能,自动上传图片并替换为Joplin链接

二、准备脚本

2.1 第一个脚本,修改token

token可以在网页剪藏器中获取

在这里插入图片描述

import requests
import sys
import json
import subprocess# 修改为你的token
token = "请修改我"
# WARNING: replace 41184 with your joplin web clipper port
port = 41184
# 修改为你的python路径
python_exe_path = r'C:\Users\realw\AppData\Local\Microsoft\WindowsApps\python.exe'
# 修改为你的第二个脚本路径
second_script_path = r'D:\app\Typora\job\typora-windows-2.py'# collect args for typora-windows-2.py
args = []
for arg in sys.argv:# the first arg is .py file, ignore itif(".py" in arg):continue# the second arg is the current editing file passed by Typoraif(".md" in arg):args.append(arg)continue# other arg is the local Typora-Image path passed by Typoraurl = "http://127.0.0.1:" + str(port) + "/resources/?token=" + tokenpayload = {'props': '{}'}files = [('data', ('typora.png', open(arg, 'rb'), 'image/png'))]headers = {}# upload your images to joplin resources using joplin web clipper APIresponse = requests.request("POST", url, headers=headers, data=payload, files=files)newFilePath = "http://127.0.0.1:" + str(port) + "/resources/" + json.loads(response.text)["id"] + "/file?token=" + token# print newFilePath, so Typora can replace the local-typora-image path with the joplin web clipper url.# We cannot directly use "resources/" + json.loads(response.text)["id"] + ".png" since Typora thinks it is an illegal URL,# and a warning will show up.print(newFilePath)args.append(newFilePath)args.append("resources/" + json.loads(response.text)["id"] + ".png")# first arg is .py file, second arg is .md file, then newFilePath and "resources/" + json.loads(response.text)["id"] + ".png" appear in order
# We use subprocess because Typora will wait for the result of the first script and then replace the Typora_local_path with newFilePath.
# If we don't use subprocess, our replacement will be ahead of Typora's, and covered by Typora, which makes newFilePath as the final result link.
# This is not what we want, we want "resources/" + json.loads(response.text)["id"] + ".png" as the result.
subprocess.Popen([python_exe_path, second_script_path] + args, creationflags=subprocess.DETACHED_PROCESS, shell=False)sys.exit()

2.1 准备第二个脚本,按需修改delay时间

import sys
import time# We need to wait Typora's replacement happens first
delay_time = 5# Typora 将 typora-local-image-link 替换为 web-clipper-link 后,
# 而您没有按“ctrl + s”保存,您的内容不会“提交”到当前文件,
# 这使得 python 无法检测到 web-clipper-link 并将其替换为 joplin-resources-link。
# 因此我们至少重试该函数 10 次以等待您的保存。(如果您在 50 秒内没有按“ctrl + s”保存,则替换将失败)
retry_times = 10
time.sleep(delay_time)for i in range(retry_times):try:with open(sys.argv[1], 'r+', encoding='utf-8') as file:file_content = file.read()file_content_copy = file_contentfor index, arg in enumerate(sys.argv):if ((".png" in arg) or (".md" in arg) or (".py" in arg)):continue# replace newFilePath with "resources/{id}.png"file_content = file_content.replace(sys.argv[index], sys.argv[index + 1])file.seek(0)file.write(file_content)file.truncate()time.sleep(delay_time)except Exception as e:print(f"An error occurred: {e}")time.sleep(delay_time)
sys.exit()

3. 修改Typora配置

在这里插入图片描述

选择图象,上传服务,命令,确保你的python环境和模块正确

python D:\app\Typora\job\typora-windows.py ${filepath}

点击验证图片上传
在这里插入图片描述

4 在编辑器内测试

用法:

在编辑器内粘贴图片,按下ctrl+s 保存,等待几秒后自动替换为内部连接,大公告成

如果遇到问题,可以手动点击上传图片,检查顶部报错

在这里插入图片描述

相关文章:

  • Java类加载大冒险,谁能将它变成漫画
  • 【网络安全的神秘世界】Kali 自带 Burp Suite 使用指南:字体与CA证书设置详解等
  • LeetCode题练习与总结:二叉树中的最大路径和--124
  • pytorch中,load_state_dict和torch.load的区别?
  • JSONObject.toJSONString(***) json化后的值中的日期值被转换为时间戳?如何修改?
  • 源码文章上传无忧,论坛小程序支持
  • 人工智能GPT-4o?
  • 【AI基础】第三步:纯天然保姆喂饭级-安装并运行chatglm2-6b
  • 大型零售企业总部到分公司数据发放,有没有更优化的方案?
  • 知识图谱的应用---新零售
  • 【ARM Cache 及 MMU 系列文章 6 -- Cache 寄存器 CTR_EL0 | CLIDR | CCSIDR | CSSELR 使用详解 1】
  • SwiftUI 利用 Swizz 黑魔法为系统创建的默认对象插入新协议方法(六)
  • 小心人工智障
  • 【氵】Archlinux+KDE Plasma 6+Wayland 安装nvidia驱动 / 开启HDR
  • 正大国际期货:如何培养个好心态呢?
  • Cookie 在前端中的实践
  • JavaScript 奇技淫巧
  • JAVA多线程机制解析-volatilesynchronized
  • k8s 面向应用开发者的基础命令
  • laravel with 查询列表限制条数
  • Linux快速复制或删除大量小文件
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • mac修复ab及siege安装
  • maven工程打包jar以及java jar命令的classpath使用
  • Python利用正则抓取网页内容保存到本地
  • Spark in action on Kubernetes - Playground搭建与架构浅析
  • Spark VS Hadoop:两大大数据分析系统深度解读
  • sublime配置文件
  • Transformer-XL: Unleashing the Potential of Attention Models
  • Web标准制定过程
  • 得到一个数组中任意X个元素的所有组合 即C(n,m)
  • 对话:中国为什么有前途/ 写给中国的经济学
  • 排序算法之--选择排序
  • 一文看透浏览器架构
  • 移动端 h5开发相关内容总结(三)
  • 阿里云服务器购买完整流程
  • 东超科技获得千万级Pre-A轮融资,投资方为中科创星 ...
  • ​ 无限可能性的探索:Amazon Lightsail轻量应用服务器引领数字化时代创新发展
  • ​​​​​​​开发面试“八股文”:助力还是阻力?
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • ​软考-高级-信息系统项目管理师教程 第四版【第23章-组织通用管理-思维导图】​
  • ​水经微图Web1.5.0版即将上线
  • # C++之functional库用法整理
  • #Ubuntu(修改root信息)
  • #绘制圆心_R语言——绘制一个诚意满满的圆 祝你2021圆圆满满
  • #我与Java虚拟机的故事#连载10: 如何在阿里、腾讯、百度、及字节跳动等公司面试中脱颖而出...
  • (Mac上)使用Python进行matplotlib 画图时,中文显示不出来
  • (使用vite搭建vue3项目(vite + vue3 + vue router + pinia + element plus))
  • (数据结构)顺序表的定义
  • (一)ClickHouse 中的 `MaterializedMySQL` 数据库引擎的使用方法、设置、特性和限制。
  • (一)kafka实战——kafka源码编译启动
  • ..回顾17,展望18
  • .bat批处理(九):替换带有等号=的字符串的子串
  • .dwp和.webpart的区别
  • .NET 2.0中新增的一些TryGet,TryParse等方法