跳转到主要内容

markdown-it 扩展

markdown.it 是 Discourse 所使用的 CommonMark 引擎,它拥有大量插件

标题锚点、定义列表、智能箭头,这样的插件还有很多。

首先是警告

:warning: CommonMark 的本意是…通用。你偏离规范越远,你的 Markdown 就越不通用。这会让移植到其他解决方案变得更困难,而且稍有不慎还会导致解析上的内部不一致。在重新打包任何东西之前,请务必回答这个问题:“我真的想要重新打包它吗?”

我刚刚完成了 https://meta.discourse.org/t/discourse-footnote/84533 的重新打包,有一些关于如何正确做到这一点的经验可以分享。

懒人的步骤

如果你很懒,只想快速上手,最简单的方法就是 fork footnotes,然后替换文件和变量名。我相当用心地确保它遵循了最佳实践,所以你会得到一个可靠的示例。

开局操作,一个最小的重新打包

据我所知,大多数 markdown.it 插件都是以原生 js 文件的形式发布的。在很多情况下,插件只是作为单个 js 文件发布,例如:markdown-it-mark.js。

理想情况下,你会希望让原始文件保持不动,这意味着你可以直接把该文件的更新版本复制到插件中,而无需改动现有插件。

你会遇到的第一个问题是,必须教会你的插件在服务器端加载这段 JavaScript,因为 Markdown 引擎同样运行在服务器上。要做到这一点,你可以直接将该文件原样复制到 assets/javascripts/vendor/original-plugin.js 中,然后在你的 plugin.rb 文件中添加:

# this teaches our markdown engine to load your vanilla js file
register_asset "javascripts/vendor/original-plugin.js", :vendored_pretty_text

一旦插件的实际主体被包含进来,你就需要教会我们的流水线如何加载和初始化它:

创建一个名为 assets/javascripts/lib/discourse-markdown/your-extension.js 的文件

这个文件会被自动加载,因为它以 .js 结尾并且位于 discourse-markdown 目录中。

一个简单的示例可以是:

export function setup(helper) {
  // this allows you only to load your extension if a site setting is enabled
  helper.registerOptions((opts, siteSettings) => {
    opts.features["your-extension"] = !!siteSettings.enable_my_plugin;
  });

  // allow any attributes that you need to support,
  // otherwise our sanitizer will strip them
  helper.allowList(["div.amazingness"]);

  // you can also do fancy stuff like this
  helper.allowList({
    custom(tag, name, value) {
      if ((tag === "a" || tag === "li") && name === "id") {
        return !!value.match(/^fn(ref)?\d+$/);
      }
    },
  });

  // finally this is the magic that you would use to register the extension in
  // our pipeline. whateverGlobal is the name of global the plugin exposes
  // it takes in a single (md) var that is then used to amend the pipeline
  helper.registerPlugin(window.whateverGlobal);
}

始终保持测试

Discourse 的 bin/rake autospec 具备插件感知能力 :innocent:

这意味着当你添加文件 spec/pretty_text_spec.rb 后,每次保存它都会运行插件测试文件。

我大量使用这个功能,因为它能让工作快得多。

假设你添加了一个把帖子中的每个数字都变成带圈的 8 的插件,你可以把它命名为 discourse-magic-8-ball。

下面是我组织测试的方式:

require "rails_helper"

describe PrettyText do
  it "can be disabled" do
    SiteSetting.enable_magic_8_ball = false

    markdown = <<~MD
      1 thing
    MD

    html = <<~HTML
      <p>1 thing</p>
    HTML

    cooked = PrettyText.cook markdown.strip
    expect(cooked).to eq(html.strip)
  end

  it "supports magic 8 ball" do
    markdown = <<~MD
      1 thing
    MD

    html = <<~HTML
      <p>8 circle thing</p>
    HTML

    cooked = PrettyText.cook markdown.strip
    expect(cooked).to eq(html.strip)
  end
end

你可能需要“装饰帖子”

在某些情况下,插件在为帖子添加额外“动态”功能时效果最好。这方面的例子有 poll 插件,或者添加了一个 "..." 并动态显示工具提示的 footnotes 插件。

如果你需要“装饰”帖子,请添加 assets/javascripts/api-initializers/your-initializer.js

import { apiInitializer } from "discourse/lib/api";

export default apiInitializer((api) => {
  const siteSettings = api.container.lookup("service:site-settings");
  if (!siteSettings.enable_magic_8_ball) {
    return;
  }

  api.decorateCookedElement((elem) => {
    // your amazing magic goes here
  });
});

你可能需要“后处理”帖子

在某些情况下,你可能需要“后处理”帖子,Markdown 渲染引擎在设计上并不感知某些信息,例如 post_id。在某些情况下,你可能希望在服务器端访问帖子以及 "cooked" HTML,这可以让你执行诸如触发后台作业、同步自定义字段或“修正”自动生成的 HTML 之类的操作。

对于脚注,我需要为每个脚注提供各不相同的 id,这意味着我需要访问 post_id,所以我被迫在后处理器(它运行在 sidekiq 中)里对 HTML 进行修改。

要接入钩子,你需要在你的 plugin.rb 文件中添加以下内容:

DiscourseEvent.on(:before_post_process_cooked) do |doc, post|
  doc.css("a.always-bing").each do |a|
    # this should always go to bing
    a["href"] = "https://bing.com"
  end
end

你可能需要一些自定义 CSS

如果你想发布自定义 CSS,请确保在 plugin.rb 中注册该文件

将你的 CSS 添加到 assets/stylesheets/magic.scss,然后运行

register_asset "stylesheets/magic.scss"

请记住,我们会“自动重新加载”更改,因此你可以修改插件的 CSS,并在开发过程中实时看到更改。

祝你重新打包顺利 :four_leaf_clover: