How Code Color is Set in StackOverflow?

前端 未结 3 468
清歌不尽
清歌不尽 2021-02-01 09:01

The most favorite feature of StackOverflow for me is that it can automatically detect code in post and set appropriate color to the code.

I\'m wondering how the color is

3条回答
  •  半阙折子戏
    2021-02-01 09:25

    As said by dbr, it does a dummy highlight of most common constructs of languages. Which doesn't work well with some exotic syntaxes. I wonder if we can do code sections without highlighting, BTW.

    For id = 1 To 10 Do
      CallSomething() // It likes CamelCase identifiers...
    End
    
    for id = 1 to 10 do # Also highlight some common keywords...
      if id % 2 then call_something(); x++; end
    end
    
    'str' "str" 12 + 15.6 * -7e+9 /* Some common constant forms */
    =/regex/ ~/regex/ +/RE/ !/regexpr/ but not /[^regex]/ (no operator before it)
    
    (* Some comment conventions are overlooked... *)
    -- Lua's (and some other languages')
    ; So are these comments...
    ' And these (Basic)
    

    Works well enough for common code to make illusion, and better than loading dozen of syntaxes in the browser.

提交回复
热议问题