文章

记录使用jekyll-compose的一个坑

问题背景

在使用jekyll-composegithub)时,我需要创建好草稿后自动打开,这个功能在README中表述如下


auto-open new drafts or posts in your editor

1
2
  jekyll_compose:
    auto_open: true

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
D:\DEVELOPTOOL\RUBY32-X64\LIB\RUBY\GEMS\3.2.0\GEMS\JEKYLL-COMPOSE-0.12.0
└─lib
    │  jekyll-compose.rb
    │
    ├─jekyll
    │  └─commands
    │          compose.rb
    │          draft.rb
    │          page.rb
    │          post.rb
    │          publish.rb
    │          rename.rb
    │          unpublish.rb
    │
    └─jekyll-compose
            arg_parser.rb
            file_creator.rb
            file_editor.rb
            file_info.rb
            file_mover.rb
            movement_arg_parser.rb
            version.rb

可以看到commands目录存放各个子命令的源码,在jekyll-compose目录中有一个file-editor.rb文件,这应该就是处理文件编辑器的源码,代码量不大,如下所示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

#
# This class is aimed to open the created file in the selected editor.
# To use this feature specify at Jekyll config:
#
# ```
#  jekyll_compose:
#    auto_open: true
# ```
#
# And make sure, that you have JEKYLL_EDITOR, VISUAL, or EDITOR environment variables set up.
# This will allow to open the file in your default editor automatically.

module Jekyll
  module Compose
    class FileEditor
      class << self
        attr_reader  :compose_config
        alias_method :jekyll_compose_config, :compose_config

        def bootstrap(config)
          @compose_config = config["jekyll_compose"] || {}
        end

        def open_editor(filepath)
          run_editor(post_editor, File.expand_path(filepath)) if post_editor
        end

        def run_editor(editor_name, filepath)
          system("#{editor_name} #{filepath}")
        end

        def post_editor
          return unless auto_open?

          ENV["JEKYLL_EDITOR"] || ENV["VISUAL"] || ENV["EDITOR"]
        end

        def auto_open?
          compose_config["auto_open"]
        end
      end
    end
  end
end

源码使用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,此时文件成功打开并且没有报错,问题解决

本文由作者按照 CC BY 4.0 进行授权