Skip to content

Commit

Permalink
Minor tweaks to attribute additions to <pre> and <code>. Removes stri…
Browse files Browse the repository at this point in the history
…ng support. Add tests.
  • Loading branch information
zachleat committed May 23, 2021
1 parent ccd0adb commit fa14c60
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 15 deletions.
2 changes: 2 additions & 0 deletions .eleventy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = {
alwaysWrapLineHighlights: false,
// eligible to change the default to \n in a new major version.
lineSeparator: "<br>",
preAttributes: {},
codeAttributes: {}
}, options);

// TODO hbs?
Expand Down
4 changes: 3 additions & 1 deletion src/CharacterWrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ class CharacterWrap {
}
}

let highlightedContent = HighlightPairedShortcode(content, codeFormat, "", { trim: false });
let highlightedContent = HighlightPairedShortcode(content, codeFormat, "", {
trim: false
});
let {document} = parseHTML(`<html><body>${highlightedContent}</body></html>`);
let counter = new IndexCounter();
let bodyEl = document.getElementsByTagName("body")[0];
Expand Down
4 changes: 2 additions & 2 deletions src/HighlightPairedShortcode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const HighlightLinesGroup = require("./HighlightLinesGroup");
const getAttributes = require("./getAttributes");

module.exports = function (content, language, highlightNumbers, options = {}) {
const preAttributes = getAttributes(options.preAttributes)
const codeAttributes = getAttributes(options.codeAttributes)
const preAttributes = getAttributes(options.preAttributes);
const codeAttributes = getAttributes(options.codeAttributes);

// default to on
if(options.trim === undefined || options.trim === true) {
Expand Down
13 changes: 5 additions & 8 deletions src/getAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,29 @@ function attributeEntryToString([key, value]) {
/**
* ## Usage
* The function `getAttributes` is used to convert an object, `attributes`, with HTML attributes as keys and the values as the corresponding HTML attribute's values.
* If the `attributes` parameter is a string, it will be returned. If it is falsey, an empty string will be returned.
* If it is falsey, an empty string will be returned.
*
* ```js
getAttributes('tabindex="0" data-language="HTML"')
// => ' tabindex="0" data-language="HTML"'
getAttributes({
tabindex: 0,
'data-language': 'JavaScript',
'data-otherStuff': 'value'
}) // => ' tabindex="0" data-language="JavaScript" data-otherStuff="value"'
```
*
* @param {string | {[s: string]: string | number}} attributes A string containing HTML attributes or an object with key-value pairs that represent attributes.
* @param {{[s: string]: string | number}} attributes An object with key-value pairs that represent attributes.
* @returns {string} A string containing the above HTML attributes preceded by a single space.
*/
function getAttributes(attributes) {
if (!attributes) {
return "";
} else if (typeof attributes === "string") {
return ` ${attributes}`;
} else if (typeof attributes === "object") {
const formattedAttributes = Object.entries(attributes).map(
attributeEntryToString
);
return ` ${formattedAttributes.join(" ")}`;
return formattedAttributes.length ? ` ${formattedAttributes.join(" ")}` : "";
} else if (typeof attributes === "string") {
throw new Error("Syntax highlighter plugin custom attributes on <pre> and <code> must be an object. Received: " + JSON.stringify(attributes));
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/markdownSyntaxHighlightOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ const HighlightLinesGroup = require("./HighlightLinesGroup");
const getAttributes = require("./getAttributes");

module.exports = function (options = {}) {
const preAttributes = getAttributes(options.preAttributes)
const codeAttributes = getAttributes(options.codeAttributes)
const preAttributes = getAttributes(options.preAttributes);
const codeAttributes = getAttributes(options.codeAttributes);

return function(str, language) {
if(!language) {
Expand Down
4 changes: 2 additions & 2 deletions test/CharacterWrapTest.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions test/GetAttributesTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const test = require("ava");
const ga = require("../src/getAttributes");

test("Falsy", t => {
t.is(ga(false), "");
t.is(ga(null), "");
t.is(ga(undefined), "");
t.is(ga(""), "");
t.throws(() => {
ga(" test='1'");
});
});

test("Object syntax", t => {
t.is(ga({}), "");
t.is(ga({ hi: 1 }), ' hi="1"');
t.is(ga({ hi: 1, bye: 2 }), ' hi="1" bye="2"');
});

0 comments on commit fa14c60

Please sign in to comment.