问题背景
在使用 jekyll-compose
(github)时,我需要创建好草稿后自动打开,这个功能在 README 中表述如下
auto-open new drafts or posts in your editor
1 | jekyll_compose: |
and make sure that you have EDITOR
, VISUAL
or JEKYLL_EDITOR
environment variable set. For instance if you wish to open newly created Jekyll posts and drafts in Atom editor you can add the following line in your shell configuration:
1 | export JEKYLL_EDITOR=atom |
JEKYLL_EDITOR
will override default EDITOR
or VISUAL
value. VISUAL
will override default EDITOR
value.
大意就是在 _config.yml
中配置 auto_open: true
,在通过 JEKYLL_EDITOR
环境变量指定打开的编辑器,于是我设置 JEKYLL_EDITOR=code
,用 vscode 打开,这样可以正常运行
但我经常使用 typora 来编辑 md 文档,因此我设置 JEKYLL_EDITOR=typora
,此时文档不能自动打开,设置 JEKYLL_EDITOR=D:\Typora\Typora.exe
,此时文档可以打开,但控制台会卡住并且报错
我已经设置了 md 文件的默认打开方式为 typora,因此我希望它按照我设置的默认打开方式打开
解决方法
要使用编辑器打开,那么源码中必然有执行一条指令调用了编辑器,于是我找到了 jekyll-compose
包的所在位置,文件树如下
1 | D:\DEVELOPTOOL\RUBY32-X64\LIB\RUBY\GEMS\3.2.0\GEMS\JEKYLL-COMPOSE-0.12.0 |
可以看到 commands 目录存放各个子命令的源码,在 jekyll-compose 目录中有一个 file-editor.rb
文件,这应该就是处理文件编辑器的源码,代码量不大,如下所示
1 | # frozen_string_literal: true |
源码使用 ruby 语言,我并没有学过 ruby 语言,但通过命名可以看出一些功能
run_editor
函数中调用了 system("#{editor_name} #{filepath}")
,这很明显是通过系统命令行来调用编辑器,那么 editor_name
就是环境变量指定的编辑器
run_editor
函数在 open_editor
函数中调用,传入的 editor_name
参数通过 post_editor
函数得到,而 post_editor
函数中就是读取了 JEKYLL_EDITOR
环境变量
现在,我想使用默认打开方式打开文件,在 Windows 中打开文件的命令是 start
,因此就将 JEKYLL_EDITOR
环境变量设为 start
,此时文件成功打开并且没有报错,问题解决