Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: multiple highlight colors #10

Open
Thylane opened this issue Jul 3, 2022 · 1 comment
Open

Feature Request: multiple highlight colors #10

Thylane opened this issue Jul 3, 2022 · 1 comment

Comments

@Thylane
Copy link

Thylane commented Jul 3, 2022

Description: Support multiple highlight colors.
For example, the code block below specify the 2nd row is highlighted in red, the 3rd in green, the 4th in blue.

```java TI:"Hello World" HL:"R[2], G[3], B[4]"

It's userful when I want to highlight the change of code, like what github does.

@Thylane
Copy link
Author

Thylane commented Jul 3, 2022

Actually I have changed the code to make this work for me, now the rendered code block looks like this:

Here's the detail:

Check the DOM
<!-- In my case the line height is 1.5em -->
<div class="code-block-highlight-wrap color-r">     <!-- red color  -->
  <span style="margin-top: 1.5em;"></span>          <!-- highlight the 2nd line -->
</div>
<div class="code-block-highlight-wrap color-g">     <!-- green color -->
  <span style="margin-top: 3em;"></span>            <!-- highlight the 3rd line -->
</div>
<div class="code-block-highlight-wrap color-b">     <!-- blue color -->
  <span style="margin-top: 4.5em;"></span>          <!-- highlight the 4th line -->
</div>
Modification to AnalyseHighLightLines
function analyseHighLightLines(str) {
  str = str.replace(/\s*/g, "");
  let hlLines = {};
  let matches = [...str.matchAll(/(R|G|B)\[(.*?)\]/g)];   // Get colors and corresponding lines.
  matches.forEach(match => {
    hlLines[match[1]] = (hlLines[match[1]] || []).concat(doAnalyse(match[2]));
    // doAnalyse does the same thing as the original function (analyseHighLightLines)
  });
  return hlLines;
  // Note that now it's not compatible with the old highlight syntax.
}
Modification to AddLineHighLight
function addLineHighLight(plugin, preElm, cbMeta) {
  let colors = Object.keys(cbMeta.highLightLines);
  let lineHeight = 1.5;   // assume it's 1.5em

  colors.forEach(color => {
    let highLightWrap = document.createElement("div");
    highLightWrap.className = "code-block-highlight-wrap color-" + color.toLowerCase();

    let indice = cbMeta.highLightLines[color].sort((a,b) => a-b);   // to make margin-top work properly
    let lastI = 0;
    for (let i of indice) {
      let singleLine = document.createElement("span");
      singleLine.style.marginTop = ((i - lastI - 1) * lineHeight) + "em";   // position the highlight element to the specified line
      lastI = i;
      highLightWrap.appendChild(singleLine);
    }
    preElm.appendChild(highLightWrap);
  });
  // Note that I ignore the plugin setting and I set the colors in css (not present here).
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant