-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add support for passing custom
preAttributes
and `codeAttribu…
…tes` options (#10)
- Loading branch information
1 parent
d7a784d
commit 00c8d2c
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
function attributeEntryToString([key, value]) { | ||
if (typeof value !== "string" && typeof value !== "number") | ||
throw new Error( | ||
`Attribute "${key}" must have a value of type string or number not "${typeof value}".` | ||
); | ||
|
||
return `${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 it is falsey, an empty string will be returned. | ||
* | ||
* ```js | ||
getAttributes({ | ||
tabindex: 0, | ||
'data-language': 'JavaScript', | ||
'data-otherStuff': 'value' | ||
}) // => ' tabindex="0" data-language="JavaScript" data-otherStuff="value"' | ||
``` | ||
* | ||
* @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 === "object") { | ||
const formattedAttributes = Object.entries(attributes).map( | ||
attributeEntryToString | ||
); | ||
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)); | ||
} | ||
} | ||
|
||
module.exports = getAttributes; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters