Skip to main content

Publish markdown to Zhihu

Project description

Inconvenience

选择mdnice或zhihu格式

今天这个项目依赖mdnice来转化公式,但是从mdnice复制到知乎编辑器时,所有H1、H2、H3标题通通会被变成H1。mdnice没有做错,应该是知乎编辑器的问题。

所以,对于没有公式的文章,请不加"--mdnice" flag,直接上传生成的md到知乎。

对于有公式的文章,用上"--mdnice" flag。

功能

这个工具帮助将Obsidian风格的Markdown文件转化为mdnice的格式,主要有以下功能

  • 自动上传本地图片到Azure blob storage
  • 保证每个image link都在每行的最开始,没有额外的空格
  • 自动为每个list item前面增加额外的回车,不然知乎不识别
  • 转换Obsidian的图片链接![[attachments/Pasted image 20230511181810.png]]到知乎格式

想要转化为知乎格式,其实还需要下面几个工作,不过我们依赖mdnice来做下面的转换。

  • 自动转换latex公式

安装

首先自行创造Azure storage account和blob container

设置环境变量IMAGEHOST_CONN_STR,这个变量要求包含形如AccountName=(.*?);.*EndpointSuffix=(.*?)($|;)")的信息

安装工具

pipx install publish-to-zhihu

如果过去安装过,可以用下面的命令升级

pipx upgrade publish-to-zhihu

日常使用

生成知乎格式的md文件并上传所有图片

假设你有多个md文件需要发布,下面是命令

# Convert standard Markdown file to Zhihu Format and upload all local images
zhihu_prepare_md --container container_name image_link_root output_folder md_file0 md_file1

其中

  • container_name是Azure blob container的名字,假设为publishpic
  • 本工具假设图像是相对路径,如![[attachments/Pasted image 20230511181810.png]]。假设在你硬盘上的真实路径是C:\Cloud\OneDrive\note\attachments\Pasted image 20230511181810.png,那么image_link_root就是C:\Cloud\OneDrive\note
  • output_folder是知乎格式md文件存放的目录。

所以,我自己发布《曼昆经济学》成本笔记.md时用的命令就是

zhihu_prepare_md --container publishpic c:\Cloud\OneDrive\note\ c:\Cloud\OneDrive\published_to_zhihu\  C:\Cloud\OneDrive\note\经济学\《曼昆经济学》成本笔记.md 

发布到知乎

发布

  • 访问c:\Cloud\OneDrive\published_to_zhihu\ 找到刚刚生成的文件
  • 打开mdnice,把md文件拷贝进去。然后从mdnice拷贝出渲染好的适合知乎的内容
  • 打开知乎,发表新文章,粘贴从mdnice得到的内容
  • 保证知乎成功导入图片:知乎会从图床中导入图片,有的时候图片上传不成功,需要手工点击图片重试一次
  • 手工填写标题
  • 对于长文章,点击“目录”按钮来生成目录
  • 如果需要投稿到问题,手工操作
  • 手工选择标签
  • 预览
  • 发表

修改已发表的文章

  • 点击文章的修改按钮或链接
  • 清空文章的所有内容
  • 粘贴从mdnice得到的内容
  • 重新导入本地生成的md文件
  • 保证知乎成功导入图片
  • 预览
  • 发表

进一步简化从Obisidan中发布文件的流程

安装Obsidian Shell Commands插件,增加一个下面的命令

zhihu_prepare_md --container publishpic c:\Cloud\OneDrive\note\ c:\Cloud\OneDrive\published_to_zhihu\  {{file_path:absolute}}

这样,在Obisidian中打开需要发布的文件,然后打开command palette,搜索"zhihu"命令运行就能够生成知乎格式的md文件了。

Setup Dev Environment

First clone this repo then change to the repo directory.

Then run following command:

pip install poetry
poetry install   # Create virtual environement, install all dependencies for the project
poetry shell     # activate the virtual environment
pre-commit install    # to ensure automatically formatting, linting, type checking and testing before every commit

If you want to run unit test manually, just activate virtual environment and run:

pytest

本地开发流程

测试脚本是否正确

python -m publish_to_zhihu.prepare_md --container publishpic c:\Cloud\OneDrive\note\ c:\Cloud\OneDrive\published_to_zhihu\ C:\Cloud\OneDrive\note\经济学\《曼昆经济学》成本笔记.md 

发布到pipx

  • bump version
  • 发布
poetry publish --build --username %PYPI_USERNAME% --password %PYPI_PASSWORD%
  • 本地升级最新版
pipx upgrade publish-to-zhihu

Acknowledgement

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

publish_to_zhihu-0.1.4.tar.gz (7.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

publish_to_zhihu-0.1.4-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file publish_to_zhihu-0.1.4.tar.gz.

File metadata

  • Download URL: publish_to_zhihu-0.1.4.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.8.5 Windows/10

File hashes

Hashes for publish_to_zhihu-0.1.4.tar.gz
Algorithm Hash digest
SHA256 a43bc8ea5615b3f81ea5e8045d8745b3467609c669151826d19bc22a790da204
MD5 62d3b928c2bbca29db2d21b60efb9bab
BLAKE2b-256 f5f822b8a882b468bc051468522bcf9def8ca011b886f5417f0f03839dfcec1c

See more details on using hashes here.

File details

Details for the file publish_to_zhihu-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: publish_to_zhihu-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.4.2 CPython/3.8.5 Windows/10

File hashes

Hashes for publish_to_zhihu-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 50cd14191e78080c479e3d8f6e6b63031eac8807ce7f0257838082dbc2456579
MD5 1b11be0c329f1dd9b251b802e74b2fa9
BLAKE2b-256 fa68ae150cf1f95c8c891aa498a81c02c2f07bde4e534c96be2698c1b952b8ce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page