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

[WIP]: Gatsby remark prismjs/line range #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
99 changes: 99 additions & 0 deletions packages/gatsby-remark-prismjs/highlight-line-range.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"use strict"

const COMMENT_START = /(#|\/\/|\{\/\*|\/\*+|<!--)/
const COMMENT_END = /(-->|\*\/\}|\*\/)?/
const DIRECTIVE = /highlight-(next-line|line|start|end)/
const END_DIRECTIVE = /highlight-end/
const plainTextWithLFTest = /<span class="token plain-text">[^<]*\n[^<]*<\/span>/g

const stripComment = line =>
line.replace(
new RegExp(
`\\s*${COMMENT_START.source}\\s*${DIRECTIVE.source}\\s*${
COMMENT_END.source
}`
),
""
)

const wrap = line =>
[
`<span class="gatsby-highlight-code-line">`,
`${stripComment(line)}\n`,
`</span>`,
].join("")

const getHighlights = (line, code, index) => {
const [, directive] = line.match(DIRECTIVE)

switch (directive) {
case "next-line":
return [
{
code: code[index + 1],
highlighted: true,
},
index + 1,
]

case "start":
const endIndex = code.findIndex(line => END_DIRECTIVE.test(line))
const end = endIndex === -1 ? code.length : endIndex
const highlighted = code.slice(index + 1, end).map(line => ({
code: wrap(line),
highlighted: true,
}))
return [highlighted, end]

case "line":
default:
return [
{
code: wrap(line),
highlighted: true,
},
index,
]
}
}

module.exports = function highlightLineRange(code, highlights = []) {
let highlighted = []
const split = code.split("\n")

if (highlights.length > 0) {
// HACK split plain-text spans with line separators inside into multiple plain-text spans
// separatered by line separator - this fixes line highlighting behaviour for jsx
code = code.replace(plainTextWithLFTest, match =>
match.replace(/\n/g, `</span>\n<span class="token plain-text">`)
)
return split.map((line, i) => {
if (highlights.includes(i + 1)) {
return {
highlighted: true,
code: line,
}
}

return {
code: line,
}
})
}

for (let i = 0; i < split.length; i++) {
const line = split[i]

if (DIRECTIVE.test(line)) {
const [highlights, index] = getHighlights(line, split, i)
highlighted = highlighted.concat(highlights)
i = index
} else {
highlighted.push({
code: line,
})
}
}

return highlighted
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!doctype html>
<html>
<head>
<title>Helloooooooooo</title>
</head>
<body>
<h1>hello world</h1> <!-- highlight-line -->
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from "react"

export default function Button() {
return (
<div>
<button>sup</button> {/* highlight-line */}
</div>
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React, { Component } from "react" // highlight-line

export default class Counter extends Component {
/* highlight-start */
state = {
count: 0,
}
// highlight-end

updateCount = () => {
this.setState(state => ({
// highlight-next-line
count: state.count + 1,
}))
}

render() {
const { count } = this.state // highlight-line
return (
<div>
<span>clicked {count}</span>
{/* highlight-start */}
<button onClick={this.updateCount}>Click me</button>
{/* highlight-end */}
</div>
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function test() {
return "hello world" // highlight-line
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function test() {
// highlight-next-line
return "hello world"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function test() {
// highlight-start
var a = "b"
return a + "hello world"
// highlight-end
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// highlight-start
var a = "b"
return a + "hello world"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- title: uno
- title: dos
- title: catorce # highlight-line
14 changes: 14 additions & 0 deletions packages/gatsby-remark-prismjs/src/__tests__/__fixtures__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const fs = require("fs-extra")
const path = require("path")

const base = __dirname

module.exports = fs.readdirSync(base).reduce((lookup, file) => {
if (file !== "index.js") {
const name = file
.replace(/-(\w)/g, (_, char) => char.toUpperCase())
.replace(/\..+/, "")
lookup[name] = fs.readFileSync(path.join(base, file), "utf8")
}
return lookup
}, {})
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`highlighting a line range highlight-line highlights line 1`] = `" return 'hello world'"`;

exports[`highlighting a line range highlight-next-line highlights correct line 1`] = `" return 'hello world'"`;

exports[`highlighting a line range highlight-start / highlight-end highlights correct lines 1`] = `
" var a = 'b'
return a + 'hello world'"
`;

exports[`highlighting a line range highlight-start / highlight-end highlights without end directive 1`] = `
"var a = 'b'
return a + 'hello world'
"
`;

exports[`highlighting a line range jsx comment highlights comment line 1`] = `" <button>sup</button>"`;

exports[`highlighting a line range yaml highlights yaml 1`] = `"- title: catorce"`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const highlightLineRange = require("../highlight-line-range")
const fixtures = require("./__fixtures__")

const output = highlighted => highlighted.map(({ code }) => code).join("\n")
const getHighlighted = lines => lines.filter(line => line.highlighted)

describe("highlighting a line range", () => {
describe("highlight-line", () => {
it("strips directive", () => {
const highlights = highlightLineRange(fixtures.highlightLine)
expect(output(highlights)).not.toContain("highlight-line")
})

it("highlights line", () => {
const highlights = highlightLineRange(fixtures.highlightLine)

expect(output(getHighlighted(highlights))).toMatchSnapshot()
})
})

describe("highlight-next-line", () => {
it("strips directive", () => {
const highlights = highlightLineRange(fixtures.highlightNextLine)

expect(output(highlights)).not.toContain("highlight-next-line")
})

it("highlights correct line", () => {
const highlights = highlightLineRange(fixtures.highlightNextLine)

expect(output(getHighlighted(highlights))).toMatchSnapshot()
})
})

describe("highlight-start / highlight-end", () => {
it("strips directives", () => {
const highlights = highlightLineRange(fixtures.highlightStartEnd)

const code = output(highlights)
;["highlight-start", "highlight-end"].forEach(directive => {
expect(code).not.toContain(directive)
})
})

it("highlights correct lines", () => {
const highlights = highlightLineRange(fixtures.highlightStartEnd)

expect(output(getHighlighted(highlights))).toMatchSnapshot()
})

it("highlights without end directive", () => {
const highlights = highlightLineRange(fixtures.highlightStartWithoutEnd)

expect(output(getHighlighted(highlights))).toMatchSnapshot()
})
})

describe("jsx comment", () => {
it("removes directive", () => {
const highlights = highlightLineRange(fixtures.highlightJsxComment)

expect(output(highlights)).not.toContain("highlight-line")
})

it("highlights comment line", () => {
const highlights = highlightLineRange(fixtures.highlightJsxComment)

expect(output(getHighlighted(highlights))).toMatchSnapshot()
})
})

describe("yaml", () => {
it("strips directive", () => {
const highlights = highlightLineRange(fixtures.highlightYaml)

expect(highlights).not.toContain("highlight-line")
})

it("highlights yaml", () => {
const highlights = highlightLineRange(fixtures.highlightYaml)

expect(output(getHighlighted(highlights))).toMatchSnapshot()
})
})

describe("kitchen sink", () => {
it.skip("strips directives", () => {
const highlights = highlightLineRange(fixtures.highlightKitchenSink)

console.log(output(highlights))
})
})
})
43 changes: 13 additions & 30 deletions packages/gatsby-remark-prismjs/src/highlight-code.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const Prism = require(`prismjs`)
const _ = require(`lodash`)

const loadPrismLanguage = require(`./load-prism-language`)

const plainTextWithLFTest = /<span class="token plain-text">[^<]*\n[^<]*<\/span>/g
const highlightLineRange = require(`./highlight-line-range`)

module.exports = (language, code, lineNumbersHighlight = []) => {
// (Try to) load languages on demand.
Expand All @@ -22,35 +21,19 @@ module.exports = (language, code, lineNumbersHighlight = []) => {

const grammar = Prism.languages[language]

let highlightedCode = Prism.highlight(code, grammar, language)
if (lineNumbersHighlight.length > 0) {
// HACK split plain-text spans with line separators inside into multiple plain-text spans
// separatered by line separator - this fixes line highlighting behaviour for jsx
highlightedCode = highlightedCode.replace(plainTextWithLFTest, match =>
match.replace(/\n/g, `</span>\n<span class="token plain-text">`)
)
const highlighted = Prism.highlight(code, grammar, language)
const codeSplits = highlightLineRange(highlighted, lineNumbersHighlight)

const codeSplits = highlightedCode.split(`\n`).map((split, i) => {
if (_.includes(lineNumbersHighlight, i + 1)) {
return {
highlighted: true,
code: `<span class="gatsby-highlight-code-line">${split}\n</span>`,
}
} else {
return { code: split }
}
})
let finalCode = ``

highlightedCode = ``
const lastIdx = codeSplits.length - 1
// Don't add back the new line character after highlighted lines
// as they need to be display: block and full-width.
codeSplits.forEach((split, idx) => {
split.highlighted
? (highlightedCode += split.code)
: (highlightedCode += `${split.code}${idx == lastIdx ? `` : `\n`}`)
})
}
const lastIdx = codeSplits.length - 1
// Don't add back the new line character after highlighted lines
// as they need to be display: block and full-width.
codeSplits.forEach((split, idx) => {
split.highlighted
? (finalCode += split.code)
: (finalCode += `${split.code}${idx == lastIdx ? `` : `\n`}`)
})

return highlightedCode
return finalCode
}
Loading