Skip to content

Commit

Permalink
Merge pull request #147 from mediocre/amazon-affiliate-links
Browse files Browse the repository at this point in the history
initial commit
  • Loading branch information
freshlogic authored Dec 5, 2024
2 parents 2ed7b30 + 60eb383 commit 85987e5
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.57.0] - 2024-12-04
### Added
- Added support for amazon.com affiliate links

## [2.56.0] - 2024-11-26
### Added
- Added support for <video> tag rendering for .mp4 and .webm links
Expand Down
21 changes: 21 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,7 @@ exports.render = function(markdown, options, callback) {
md.linkify.tlds('horse', true);
md.linkify.tlds('deals', true);

md.use(require('./plugins/amazon'));
md.use(require('./plugins/anchors'), { suffix: options.id });
md.use(require('./plugins/appleMusic'));
md.use(require('./plugins/audio'));
Expand Down Expand Up @@ -786,6 +787,26 @@ exports.render = function(markdown, options, callback) {
}, function() {
callback();
});
},
// https://github.com/markdown-it/markdown-it/issues/1031
function _urlEncodeFix(callback) {
const aElements = exports.html.getElementsByTagName(html, 'a');

if (!aElements?.length) {
return callback();
}

aElements.forEach(aElement => {
const href = exports.html.getAttributeValue(aElement, 'href');

if (!href?.includes('&amp;')) {
return;
}

html = html.replaceAll(`href="${href}"`, `href="${href.replaceAll('&amp;', '&')}"`);
});

return callback();
}
], function(err) {
if (err) {
Expand Down
29 changes: 29 additions & 0 deletions lib/plugins/amazon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const regExp = /https?:\/\/(?:[a-z0-9-]+\.)?amazon\.com(?:\/[^\s]*)?(?=\s|$)/i;

module.exports = function(md) {
const defaultRender = md.renderer.rules.link_open || function(tokens, idx, options, env, self) {
return self.renderToken(tokens, idx, options);
};

md.renderer.rules.link_open = function(tokens, idx, options, env, self) {
const openToken = tokens[idx];
const hrefIndex = openToken.attrIndex('href');

// Ensure we have an "href" attribute
if (hrefIndex !== -1) {
const href = openToken.attrs[hrefIndex][1];

// Ensure we have an "href" attribute value that matches the Amazon URL pattern
if (href && regExp.test(href)) {
const url = new URL(href);

if (!url.searchParams.has('tag')) {
url.searchParams.set('tag', 'mehdown-20');
openToken.attrs[hrefIndex][1] = url.toString();
}
}
}

return defaultRender(tokens, idx, options, env, self);
};
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"async": "~3.2.0",
"async": "~3.2.6",
"emoji-toolkit": "~6.6.0",
"flip": "~1.0.0",
"lolspeak": "~1.4.0",
Expand All @@ -10,7 +10,7 @@
"natural": "~5.2.4",
"probe-image-size": "~7.2.0",
"rel-to-abs": "~0.1.0",
"request": "~2.88.0",
"request": "~2.88.2",
"roll": "~1.3.1",
"shell-quote": "~1.8.0",
"slug": "~9.0.0",
Expand All @@ -33,5 +33,5 @@
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls",
"test": "mocha --reporter spec test/*"
},
"version": "2.56.0"
"version": "2.57.0"
}
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ describe('links', function() {
// https://github.com/mediocre/mehdown/issues/39
it('URLs that happen to have Emoji shortnames should be linked correctly', function(done) {
mehdown.render('http://www.ebay.com/itm/GOgroove-BlueSYNC-OR3-Portable-Wireless-Bluetooth-Speaker-Splatter-Edition-/351328294837?var=&hash=item51cccc57b5:m:mLzVeDaMfRlxsCqFiutY-aw', function(err, html) {
assert.strictEqual(html, '<p><a href="http://www.ebay.com/itm/GOgroove-BlueSYNC-OR3-Portable-Wireless-Bluetooth-Speaker-Splatter-Edition-/351328294837?var=&amp;hash=item51cccc57b5:m:mLzVeDaMfRlxsCqFiutY-aw">http://www.ebay.com/itm/GOgroove-BlueSYNC-OR3-Portable-Wireless-Bluetooth-Speaker-Splatter-Edition-/351328294837?var=&amp;hash=item51cccc57b5:m:mLzVeDaMfRlxsCqFiutY-aw</a></p>');
assert.strictEqual(html, '<p><a href="http://www.ebay.com/itm/GOgroove-BlueSYNC-OR3-Portable-Wireless-Bluetooth-Speaker-Splatter-Edition-/351328294837?var=&hash=item51cccc57b5:m:mLzVeDaMfRlxsCqFiutY-aw">http://www.ebay.com/itm/GOgroove-BlueSYNC-OR3-Portable-Wireless-Bluetooth-Speaker-Splatter-Edition-/351328294837?var=&amp;hash=item51cccc57b5:m:mLzVeDaMfRlxsCqFiutY-aw</a></p>');
done();
});
});
Expand Down
69 changes: 69 additions & 0 deletions test/plugins/amazon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const assert = require('assert');

const mehdown = require('../../lib');

describe('amazon', function() {
it('https://www.amazon.com/dp/B0D3VQ6MH5', function(done) {
mehdown.render('https://www.amazon.com/dp/B0D3VQ6MH5', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?tag=mehdown-20">https://www.amazon.com/dp/B0D3VQ6MH5</a></p>');
done();
});
});

it('https://www.amazon.com/dp/B0D3VQ6MH5?th=1', function(done) {
mehdown.render('https://www.amazon.com/dp/B0D3VQ6MH5?th=1', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?th=1&tag=mehdown-20">https://www.amazon.com/dp/B0D3VQ6MH5?th=1</a></p>');
done();
});
});

it('https://www.amazon.com/dp/B0D3VQ6MH5?th=1&foo=bar', function(done) {
mehdown.render('https://www.amazon.com/dp/B0D3VQ6MH5?th=1&foo=bar', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?th=1&foo=bar&tag=mehdown-20">https://www.amazon.com/dp/B0D3VQ6MH5?th=1&amp;foo=bar</a></p>');
done();
});
});

it('https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace', function(done) {
mehdown.render('https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace">https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace</a></p>');
done();
});
});

it('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5)', function(done) {
mehdown.render('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5)', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?tag=mehdown-20">amazon link</a></p>');
done();
});
});

it('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5?th=1)', function(done) {
mehdown.render('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5?th=1)', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?th=1&tag=mehdown-20">amazon link</a></p>');
done();
});
});

it('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5?th=1&foo=bar)', function(done) {
mehdown.render('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5?th=1&foo=bar)', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?th=1&foo=bar&tag=mehdown-20">amazon link</a></p>');
done();
});
});

it('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace)', function(done) {
mehdown.render('[amazon link](https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace)', function(err, html) {
assert.ifError(err);
assert.strictEqual(html, '<p><a href="https://www.amazon.com/dp/B0D3VQ6MH5?tag=donotreplace">amazon link</a></p>');
done();
});
});
});
2 changes: 1 addition & 1 deletion test/plugins/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('youtube', function() {

it('[text](http://www.youtube.com/watch?v=kU9MuM4lP18&start=10&end=20)', function(done) {
mehdown.render('[text](http://www.youtube.com/watch?v=kU9MuM4lP18&start=10&end=20)', function(err, html) {
assert.equal(html, '<p><a href="http://www.youtube.com/watch?v=kU9MuM4lP18&amp;start=10&amp;end=20">text</a></p>');
assert.equal(html, '<p><a href="http://www.youtube.com/watch?v=kU9MuM4lP18&start=10&end=20">text</a></p>');
done();
});
});
Expand Down

0 comments on commit 85987e5

Please sign in to comment.