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

Add line numbers #24

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const markdownPrismJs = require("./src/markdownSyntaxHighlightOptions");
module.exports = {
initArguments: { Prism },
configFunction: function(eleventyConfig, options = {}) {
options = Object.assign({ alwaysWrapLineHighlights: false }, options);
options = Object.assign({ alwaysWrapLineHighlights: false,
showLineNumbers: false}, options);

// TODO hbs?
if( hasTemplateFormat(options.templateFormats, "liquid") ) {
Expand All @@ -26,6 +27,9 @@ module.exports = {
});
}

// this is where the plugin code gets hooked up
// in the default md's highlighter

if( hasTemplateFormat(options.templateFormats, "md") ) {
eleventyConfig.addMarkdownHighlighter(markdownPrismJs(options));
}
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# eleventy-plugin-syntaxhighlight

To show line numbers, set the `showLineNumbers` option to true.
This is a global setting.

```
module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight, {
alwaysWrapLineHighlights: true,
showLineNumbers: false
});

eleventyConfig.setTemplateFormats("njk,liquid,md,css");
};
```

A pack of [Eleventy](https://github.com/11ty/eleventy) plugins for syntax highlighting. No browser/client JavaScript here, these highlight transformations are all done at build-time.

## Read the [Full Documentation on 11ty.dev](https://www.11ty.dev/docs/plugins/syntaxhighlight/)
Expand Down
3 changes: 2 additions & 1 deletion demo/eleventy-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const syntaxHighlight = require("../.eleventy.js");

module.exports = function(eleventyConfig) {
eleventyConfig.addPlugin(syntaxHighlight, {
// alwaysWrapLineHighlights: true
alwaysWrapLineHighlights: true,
showLineNumbers: false
});

eleventyConfig.setTemplateFormats("njk,liquid,md,css");
Expand Down
54 changes: 54 additions & 0 deletions demo/line-numbers.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/***
* LINE NUMBERS
* https://raw.githubusercontent.com/PrismJS/prism/master/plugins/line-numbers/prism-line-numbers.css
***/

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;
}

.line-numbers .line-numbers-rows {
position: absolute;
pointer-events: none;
top: -4px; /* hack to get numbers to line up with text */
font-size: 100%;
left: -3.8em;
width: 3em; /* works for line-numbers below 1000 lines */
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;
}

.highlight-line-add {
background-color: rgb(197, 234, 197);
}

.highlight-line-remove {
background-color: rgb(253, 204, 204);
}
59 changes: 59 additions & 0 deletions demo/test-markdown.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,35 @@
<title></title>
<link rel="stylesheet" href="../test.css">
<link rel="stylesheet" href="../prism-theme.css">
<link rel="stylesheet" href="../line-numbers.css">
</head>
<body>


ts

```ts
function myFunction() {
return true;
}
```

typescript

```typescript
function myFunction() {
return true;
}
```

js

```js
function myFunction() {
return true;
}
```
js

```js
let multilineString = `
Expand All @@ -34,8 +43,34 @@ let multilineString = `
`;
```

## Table

markdown

``` markdown
| State | Capital |
| :------------ | :------ |
| New York | Albany |
| Nebraska | Lincoln |
| New Hampshire | Concord |
```

markdown/1,3

``` markdown/1,3
| State | Capital |
| :------------ | :------ |
| New York | Albany |
| Nebraska | Lincoln |
| New Hampshire | Concord |
```



## Dash line

js/-

```js/-
let multilineString = `
this is the first line
Expand All @@ -44,6 +79,8 @@ let multilineString = `
`;
```

js/1,3

```js/1,3
let multilineString = `
this is the first line
Expand All @@ -52,5 +89,27 @@ let multilineString = `
`;
```

text/1,3

```text/1,3
this is the first line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
```



text/1,3/2,4

```text/1,3/2,4
this is the first line
this is the second line
this is the third line
this is the fourth line
this is the fifth line
```

</body>
</html>
54 changes: 51 additions & 3 deletions src/markdownSyntaxHighlightOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@ const Prism = require("prismjs");
const PrismLoader = require("./PrismLoader");
const HighlightLinesGroup = require("./HighlightLinesGroup");

/**
*
* lang / 1,2,5-8
* highlight lines 1,2,5,6,7,8
*
* lang / 1, 9 / 2-4,6
* add lines 1, 9
* delete lines 2,3,4,6
*/

module.exports = function(options = {}) {
return function(str, language) {
let plugin = function( str, language) {

if(!language) {
// empty string means defer to the upstream escaping code built into markdown lib.
return "";
Expand All @@ -21,9 +32,26 @@ module.exports = function(options = {}) {
html = Prism.highlight(str, PrismLoader(language), language);
}

// Prism's markdown highlighter renders tables
// with a newline before the end of the corresponding
// markdown line. It's hacky, but I'd rather
// fix it up here than figure out how to
// fix Prism's markdown highlighter

if(language === 'markdown'){
let wickedPattern=/<\/span>\n<\/span>/g
let goodReplacement='</span></span>\n'
html = html.replace(wickedPattern, goodReplacement)
}

let hasHighlightNumbers = split.length > 0;
let highlights = new HighlightLinesGroup(split.join("/"), "/");
let lines = html.split("\n").slice(0, -1); // The last line is empty.

let lines = html.split("\n")
// The last line is usually empty
// but not always -- such as tables
if (lines[lines.length-1] === "")
lines.pop()

lines = lines.map(function(line, j) {
if(options.alwaysWrapLineHighlights || hasHighlightNumbers) {
Expand All @@ -33,6 +61,26 @@ module.exports = function(options = {}) {
return line;
});

return `<pre class="language-${language}"><code class="language-${language}">${lines.join("<br>")}</code></pre>`;
let lineNumbers = "" // the "<span></span>" string that does the numbering
if (options.showLineNumbers) {
lineNumbers = `<span aria-hidden="true" class="line-numbers-rows">${new Array(lines.length + 1).join("<span></span>")}</span>`
}

let classNames = `language-${language}` +
(options.showLineNumbers
? " line-numbers"
: "")
// the line-numbers CSS class shifts the lines right
// to make room for the line numbers

let retStr = `<pre class="${classNames}">` +
`<code class="language-${language}">${lines.join("<br>")}` +
lineNumbers +
`</code></pre>`

return retStr
};

return plugin;
};

11 changes: 11 additions & 0 deletions test/MarkdownHighlightTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ alert();
\`\`\``).trim(), `<pre class="language-js"><code class="language-js"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>`);
});

test("Test Markdown Highlighter Line Numbers", t => {
let mdLib = md();
mdLib.set({
highlight: markdownPrismJsOptions({ alwaysWrapLineHighlights: true,
showLineNumbers: true})
});
t.is(mdLib.render(`\`\`\`js
alert();
\`\`\``).trim(), `<pre class="language-js line-numbers"><code class="language-js"><span class="highlight-line"><span class="token function">alert</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span><span aria-hidden="true" class="line-numbers-rows"><span></span></span></code></pre>`);
});


// test("Test Markdown Highlighter Block Comment", t => {
// let mdLib = md();
Expand Down