Joplin Typora 粘贴图片 | 当使用Typora作为Joplin编辑器时,如何粘贴图片并上传到Joplin服务器,替换链接
创始人
2025-01-21 09:04:40
0次

一、背景

当我们使用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 it     if(".py" in arg):         continue     # the second arg is the current editing file passed by Typora     if(".md" in arg):         args.append(arg)         continue     # other arg is the local Typora-Image path passed by Typora     url = "http://127.0.0.1:" + str(port) + "/resources/?token=" + token     payload = {'props': '{}'}     files = [       ('data', ('typora.png', open(arg, 'rb'), 'image/png'))     ]     headers = {}     # upload your images to joplin resources using joplin web clipper API     response = 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_content             for 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 保存,等待几秒后自动替换为内部连接,大公告成

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

在这里插入图片描述

相关内容

热门资讯

实测交流透视挂!宝玛大厅ai智... 实测交流透视挂!宝玛大厅ai智能透视挂教程,宝玛大厅破解软件经验(本来有挂辅助)1、每一步都需要思考...
总算了解辅助器!朱雀吴江三缺一... 总算了解辅助器!朱雀吴江三缺一ai智能透视辅助挂技巧,朱雀吴江三缺一好像是有挂(有辅助挂细节)朱雀吴...
今日公布透视!九五至尊大厅ai... 今日公布透视!九五至尊大厅ai智能透视插件,九五至尊大厅有没有透视挂窍门(切实有挂技术)1、在九五至...
玩家必看科普开挂!聚闲麻将ai... 玩家必看科普开挂!聚闲麻将ai智能透视辅助挂软件,聚闲麻将总是真的有挂(有辅助挂方式)聚闲麻将能透视...
据报道开挂!旅游娱乐ai智能开... 据报道开挂!旅游娱乐ai智能开挂工具,旅游娱乐辅助工具演示(一直有挂教学)1、全新机制【旅游娱乐ai...
一分钟揭秘开挂!决胜朋来麻将a... 一分钟揭秘开挂!决胜朋来麻将ai智能透视辅助挂教程,决胜朋来麻将真是真的是有挂(有辅助挂总结)1、首...
如何分辨真伪透视!红牛ai智能... 如何分辨真伪透视!红牛ai智能透视攻略,红牛是不是有挂经验(真是有挂技术)1、操作简单,无需红牛手机...
2分钟细说辅助器!江湖大庆麻将... 2分钟细说辅助器!江湖大庆麻将ai智能透视辅助挂攻略,江湖大庆麻将竟然是真的有挂(有辅助挂头条)1、...
总算清楚透视挂!老夫子厅ai智... 总算清楚透视挂!老夫子厅ai智能透视挂技巧,老夫子厅怎么开挂课程(好像发现有挂)老夫子厅破解侠是真的...
解密关于辅助!菜鸟麻将ai智能... 解密关于辅助!菜鸟麻将ai智能透视辅助挂脚本,菜鸟麻将总是存在有挂(有辅助挂存在)1、用户打开应用后...