Kuma 平台为 MDN 提供了强大的宏系统——KumaScript,使得 MDN 能够自动地去做各种东西。本文提供一些信息以便大家知道如何使用 MDN 上的文章内的宏。

鉴于本文只是 KumaScript 的简介,KumaScript 指南提供了更深入的内容。


MDN使用的Macros(宏)是基于Node.js执行环境,并在服务端执行运行。这里包括了大量的代码库,另外对此还提供了丰富的wiki说明文档内容。如果你希望能学习到更多的内容,可以查看 KumaScript 指南KumaScript reference 则提供了更多关于这些代码库和KumaScript的API的实现机理。


To actually use a macro, you simply enclose the call to the macro in a pair of double-braces, with its parameters, if any, enclosed in parentheses; that is:


A few notes about macro calls:

  • Macro names are case-sensitive, but some attempt is made to correct for common capitalization errors; you may use all lowercase even if the macro name uses caps within it, and you may capitalize a macro whose name normally starts with a lower-case letter.
  • Parameters are separated by commas.
  • If there are no parameters, you may leave out the parentheses entirely; {{macroname()}} and {{macroname}} are identical.
  • Numeric parameters can be in quotes, or not. It's up to you (however, if you have a version number with multiple decimals in it, it needs to be in quotes).
  • If you get errors, review your code carefully. If you still can't figure out what's going on, see Troubleshooting KumaScript errors for help.

Macros are heavily cached; for any set of input values (both parameters and environmental values such as the URL for which the macro was run), the results are stored and reused. This means that the macro is only actually run when the inputs change.

Note: You can force all the macros on a page to be re-evaluated by force-refreshing the page in your browser (that is, a shift-reload).

Macros can be as simple as just inserting a larger block of text or swapping in contents from another part of MDN, or as complex as building an entire index of content by searching through parts of the site, styling the output, and adding links.

You can read up on our most commonly-used macros on the Commonly-used macros page; also, there's a complete list of all macros here. Most macros have documentation built into them, as comments at the top.


此页面的贡献者: wbamberg, ufoqhmdt, Ende93, jswisher
最后编辑者: wbamberg,