Skip to content

Add support for line numbers#14

Open
andeersg wants to merge 2 commits into11ty:mainfrom
andeersg:feature/linenumbers
Open

Add support for line numbers#14
andeersg wants to merge 2 commits into11ty:mainfrom
andeersg:feature/linenumbers

Conversation

@andeersg
Copy link
Copy Markdown

@andeersg andeersg commented Jun 6, 2019

I have tried for a while to use the line numbers plugin provided by Prism in Eleventy. But it's to heavily dependent on the browser so it's not possible.

Next I tried to create a plugin that could be used in plugin options for this one. But that also was unsuccessful because of the limited Prism hooks that are used by node.js.

So then I looked at original line numbers plugin and came up with this solution to add support in Eleventy.

The function LineNumbers tries to do the same as the original Prism plugin, it creates a <span> with a span per line as children.

Then I tweaked markdownSyntaxHighlight.js to use it and append the spans to the code.

This works really good with the CSS from Prism:

.line-numbers {
  .line-numbers-rows {
    position: absolute;
    pointer-events: none;
    top: 0;
    font-size: 100%;
    left: -3.8em;
    width: 3em;
    letter-spacing: -1px;
    border-right: 1px solid #999;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
  }
  .line-numbers-rows > span {
    pointer-events: none;
    display: block;
    counter-increment: linenumber;
  }
  .line-numbers-rows > span::before {
    content: counter(linenumber);
    color: #999;
    display: block;
    padding-right: 0.8em;
    text-align: right;
}
}
pre[class*="language-"].line-numbers {
  position: relative;
  padding-left: 3.8em;
  counter-reset: linenumber;
}
pre[class*="language-"].line-numbers > code {
  position: relative;
  white-space: inherit;
}

Example:
Eleventy_Essentials___Andeers_com

I'm not sure if this is the best approach, and I'm not sure what the best way to make it optional is. But I figured it was easier to create this PR to show my solution. It would fix #10.

@henrik
Copy link
Copy Markdown

henrik commented Mar 8, 2020

I'm not sure what the best way to make it optional is

FWIW, when I used Octopress, the syntax was e.g.

``` js linenos:false

to skip line numbers.

@pborenstein pborenstein mentioned this pull request May 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

How to show line numbers

2 participants