Skip to content

Commit a8ebb01

Browse files
authored
Merge pull request #50 from chriskirknielsen/master
Add context info and callback to getAttributes
2 parents 4049efc + 48dda76 commit a8ebb01

File tree

4 files changed

+23
-12
lines changed

4 files changed

+23
-12
lines changed

src/HighlightPairedShortcode.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ const HighlightLinesGroup = require("./HighlightLinesGroup");
44
const getAttributes = require("./getAttributes");
55

66
module.exports = function (content, language, highlightNumbers, options = {}) {
7-
const preAttributes = getAttributes(options.preAttributes);
8-
const codeAttributes = getAttributes(options.codeAttributes);
7+
const context = { content: content, language: language, options: options };
8+
const preAttributes = getAttributes(options.preAttributes, context);
9+
const codeAttributes = getAttributes(options.codeAttributes, context);
910

1011
// default to on
1112
if(options.trim === undefined || options.trim === true) {

src/getAttributes.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
function attributeEntryToString([key, value]) {
1+
function attributeEntryToString(attribute, context) {
2+
let [key, value] = attribute;
3+
4+
if (typeof value === "function") { // Callback must return a string or a number
5+
value = value(context); // Run the provided callback and store the result
6+
}
7+
28
if (typeof value !== "string" && typeof value !== "number")
39
throw new Error(
4-
`Attribute "${key}" must have a value of type string or number not "${typeof value}".`
10+
`Attribute "${key}" must have, or evaluate to, a value of type string or number, not "${typeof value}".`
511
);
612

713
return `${key}="${value}"`;
@@ -15,21 +21,23 @@ function attributeEntryToString([key, value]) {
1521
* ```js
1622
getAttributes({
1723
tabindex: 0,
18-
'data-language': 'JavaScript',
24+
'data-language': function (context) { return context.language; },
1925
'data-otherStuff': 'value'
2026
}) // => ' tabindex="0" data-language="JavaScript" data-otherStuff="value"'
2127
```
2228
*
2329
* @param {{[s: string]: string | number}} attributes An object with key-value pairs that represent attributes.
30+
* @param {object} context An object with the current context.
31+
* @param {string} context.content The code to parse and highlight.
32+
* @param {string} context.language The language for the current instance.
33+
* @param {object} context.options The options passed to the syntax highlighter.
2434
* @returns {string} A string containing the above HTML attributes preceded by a single space.
2535
*/
26-
function getAttributes(attributes) {
36+
function getAttributes(attributes, context) {
2737
if (!attributes) {
2838
return "";
2939
} else if (typeof attributes === "object") {
30-
const formattedAttributes = Object.entries(attributes).map(
31-
attributeEntryToString
32-
);
40+
const formattedAttributes = Object.entries(attributes).map(entry => attributeEntryToString(entry, context));
3341
return formattedAttributes.length ? ` ${formattedAttributes.join(" ")}` : "";
3442
} else if (typeof attributes === "string") {
3543
throw new Error("Syntax highlighter plugin custom attributes on <pre> and <code> must be an object. Received: " + JSON.stringify(attributes));

src/markdownSyntaxHighlightOptions.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@ const HighlightLinesGroup = require("./HighlightLinesGroup");
44
const getAttributes = require("./getAttributes");
55

66
module.exports = function (options = {}) {
7-
const preAttributes = getAttributes(options.preAttributes);
8-
const codeAttributes = getAttributes(options.codeAttributes);
9-
107
return function(str, language) {
8+
const context = { content: str, language: language, options: options };
9+
const preAttributes = getAttributes(options.preAttributes, context);
10+
const codeAttributes = getAttributes(options.codeAttributes, context);
11+
1112
if(!language) {
1213
// empty string means defer to the upstream escaping code built into markdown lib.
1314
return "";

test/GetAttributesTest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ test("Object syntax", t => {
1515
t.is(ga({}), "");
1616
t.is(ga({ hi: 1 }), ' hi="1"');
1717
t.is(ga({ hi: 1, bye: 2 }), ' hi="1" bye="2"');
18+
t.is(ga({ hi: function(ctx) { return '1'; }, bye: 2 }), ' hi="1" bye="2"');
1819
});

0 commit comments

Comments
 (0)