Help us understand the problem. What is going on with this article?

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

More than 1 year has passed since last update.

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

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした