-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add plugin to set attributes on links (#108)
- Loading branch information
Showing
5 changed files
with
139 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
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,71 @@ | ||
/* | ||
Copyright 2024 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
/** | ||
* @module modifiers/link_attributes | ||
*/ | ||
|
||
const safeObject = require('../util/safe_object'); | ||
|
||
/** | ||
* @typedef {Object} LinkAttributesOptions | ||
* @property {Object<string, string>|function(Object<string, string>): Object<string, string>} attributes Object or function to generate attributes for links. | ||
*/ | ||
|
||
/** | ||
* Apply custom attributes to all links in the Markdown content. | ||
* | ||
* If an object is provided, the provided attributes are merged with the existing attributes. | ||
* | ||
* If a function is provided, the existing attributes are passed to it, | ||
* and the existing attributes are replaced (not merged) with the return value. | ||
* | ||
* @type {import('markdown-it').PluginWithOptions<LinkAttributesOptions>} | ||
*/ | ||
module.exports = (md, options) => { | ||
// Get the correct options | ||
const optsObj = safeObject(options); | ||
|
||
/** | ||
* Wrap the link render function to inject custom attributes. | ||
* | ||
* @param {import('markdown-it/lib/renderer').RenderRule} [original] Original render function. Defaults to `renderToken`. | ||
* @returns {import('markdown-it/lib/renderer').RenderRule} | ||
* @private | ||
*/ | ||
const render = original => (tokens, idx, opts, env, self) => { | ||
// Get the token | ||
const token = tokens[idx]; | ||
|
||
// Handle a function for transforming attributes | ||
// Otherwise, merge the attributes | ||
if (typeof optsObj.attributes === 'function') { | ||
const currentAttrs = Object.fromEntries(token.attrs); | ||
token.attrs = Object.entries(optsObj.attributes(currentAttrs)); | ||
} else { | ||
token.attrs = Object.entries({ ...Object.fromEntries(token.attrs), ...optsObj.attributes }); | ||
} | ||
|
||
// Render as normal | ||
return typeof original === 'function' | ||
? original(tokens, idx, opts, env, self) | ||
: self.renderToken(tokens, idx, opts, env); | ||
}; | ||
|
||
md.renderer.rules.link_open = render(md.renderer.rules.link_open); | ||
}; |
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 @@ | ||
/* | ||
Copyright 2024 DigitalOcean | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const md = require('markdown-it')().use(require('./link_attributes')); | ||
|
||
it('does not alter links by default', () => { | ||
expect(md.render('[hello](/world "test")')).toBe(`<p><a href="/world" title="test">hello</a></p> | ||
`); | ||
}); | ||
|
||
const mdObject = require('markdown-it')({ }).use(require('./link_attributes'), { attributes: { target: '_blank', title: 'other' } }); | ||
|
||
it('handles an object of attributes', () => { | ||
expect(mdObject.render('[hello](/world "test")')).toBe(`<p><a href="/world" title="other" target="_blank">hello</a></p> | ||
`); | ||
}); | ||
|
||
// eslint-disable-next-line jsdoc/require-jsdoc | ||
const mdFunction = require('markdown-it')({ }).use(require('./link_attributes'), { attributes: attrs => ({ ...attrs, target: '_blank', title: 'other' }) }); | ||
|
||
it('handles a function returning attributes', () => { | ||
expect(mdFunction.render('[hello](/world "test")')).toBe(`<p><a href="/world" title="other" target="_blank">hello</a></p> | ||
`); | ||
}); |