LoginSignup
0
1

More than 3 years have passed since last update.

Redmineでのコード表示に行番号を付加する(IE不可)

Last updated at Posted at 2019-03-14

ViewCustomizePluginを使います。

  • Redmine3.4, 4.0
  • Chrome/Firefox/Edge

で動作確認しました。
IEでは動きませんでした…

Path pattern: /.*
Type: JavaScript

$(function(){

  // Redmine 3.x
  //const highlightClass='.CodeRay';
  // Redmine 4.x
  const highlightClass = "[class$='syntaxhl']";

  let codeBlocks = document.querySelectorAll( highlightClass );

  let LINE_DELIM_EXP = /\n(?!$)/g;

  Array.prototype.forEach.call(codeBlocks,function( codeBlock ) {

    if (codeBlock.tagName != "CODE") { return; }

    // 行数取得
    let codeLines = codeBlock.innerHTML.split(LINE_DELIM_EXP);
    // 行の桁数
    let figures = String(codeLines.length).length;

    let newCode = '';

    Array.prototype.forEach.call(codeLines , function ( codeLine , index ) {

      // 行番号をつける
      let lineStr = (Array(figures).fill(' ').join('') + ( index + 1 ) ).slice( -1 * figures );
      let lineNumber = document.createElement("span");
      lineNumber.innerHTML = lineStr + ' ';
//      lineNumber.className = 'line-numbers';
      lineNumber.style = 'padding: 2px 4px 2px 4px; background-color: #eee; margin:0px 5px 0px 0px;';

      newCode += lineNumber.outerHTML;
      newCode += codeLine;
      newCode += '\n';
    });
    codeBlock.innerHTML = newCode;

  });
});
  • 4行目/6行目はRedmineのバージョンに合わせてください。
  • 昔のRedmineのソースでは、class="line-numbers"となっていましたが、今のRedmineのCSSには存在しないようなので、適当にstyleを書いています。

こんな感じになります。
image.png

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1