Syntax highlighting - The MDN project 编辑

When you add a code example to an article on MDN, it's helpful to enable syntax highlighting, to make it easy for readers to visually pick out relevant bits of the code. The syntax highlighting mode you select is also used by our live sample rendering system to determine how to use each code snippet when assembling a complete example.

Support for syntax highlighting

MDN's wiki includes support for all of the languages we frequently use on MDN (as well as some we only use occasionally):

  • Bash shell

  • C/C++

  • CSS

  • HTML

  • Java

  • JavaScript

  • JSON

  • PHP

  • Python

  • SQL

  • XML

Syntax highlighting can help make the code more readable, especially when it's in the context of a document full of other kinds of text. It can also help identify problems with examples and code snippets.

Applying syntax highlighting

As a rule, you should always enable syntax highlighting for any <pre> block which presents code if the block is more than one line long; for one-line code blocks, syntax highlighting may be optional depending on the context.

To enable syntax highlighting in code examples:

  1. Type or paste the code into the article. For example:

    void main(int argc, char **argv) {

    printf("Hello world\n");

    }

  2. Select the text and click the PRE button in the editor toolbar. The result looks like this:

    void main(int argc, char **argv) {
    
    printf("Hello world\n");
    
    }
    
  3. Click the Syntax Highlighter button and select the name of the appropriate programming language for the code example. In this case, it is C/C++:
    void main(int argc, char **argv) {
    
    printf("Hello world\n");
    
    }

The editor does not currently display the syntax highlighting.  You'll need to preview or save your changes and look at the article to see the syntax highlighting in effect.

You can actually simplify this process slightly by selecting the language from the syntax highlighter's language list in the toolbar without first clicking the PRE button. Applying syntax highlighting automatically adds the required <pre> block if it's not already in place.

If you also need to highlight specific lines or groups of lines within the code, you can do so by making some changes in source mode. See Highlighting lines in sample code in Source mode.

See also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:145 次

字数:3840

最后编辑:7 年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文