Skip to content

Commit

Permalink
FIx lint error, add test in travis and Appveyor
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Apr 16, 2021
1 parent 5c2d9bd commit 9d6554a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ install:
npm install --dev
script:
- npm run lint
- npm run test:download
- npm run test:title
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[![Build Status][travis-image]][travis-url]
[![AppVeyor Build Status][appveyor-image]][appveyor-url]

[Remarkable](https://www.npmjs.com/package/remarkable) plugin adds `title` attribute on link and image tags.
[Remarkable](https://www.npmjs.com/package/remarkable) plugin adds `title` attribute on URL and image tags and `download` attribute on URL if `title` set to `download`.

## Install

Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ install:

test_script:
- npm run lint
- npm run test:download
- npm run test:title

build: off
25 changes: 20 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,32 @@ const remarkableSeo = (md, options) => {
return defaultImageRender(tokens, idx, ...args);
};
}
if ((config.link && config.link.includes('title')) || config.download) {
if (config.link && config.link.includes('title')) {
const defaultLinkRender = md.renderer.rules.link_open;
md.renderer.rules.link_open = (tokens, idx, ...args) => {
tokens.map((token) => {
if (token && token.title === '') {
const linkTextId = idx + 1;
if (tokens[linkTextId]) {
const linkContent = tokens[linkTextId];
if (linkContent.type === 'text') {
token.title = linkContent.content;
}
}
}
});
return defaultLinkRender(tokens, idx, ...args);
};
}
if (config.download) {
const defaultLinkRender = md.renderer.rules.link_open;
md.renderer.rules.link_open = (tokens, idx, ...args) => {
let downloadAttr = false;
tokens.map((token) => {
if (token) {
if (token.title === '' || token.title === 'download') {
if (token.title === 'download') {
const linkTextId = idx + 1;
if (config.download && token.title === 'download') {
downloadAttr = true;
}
downloadAttr = true;
if (tokens[linkTextId]) {
const linkContent = tokens[linkTextId];
if (linkContent.type === 'text') {
Expand Down
32 changes: 25 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,38 @@ const remarkableSeo = (md: Remarkable, options?: types.configOptions): void => {
};
}

if ((config.link && config.link.includes('title')) || config.download) {
if (config.link && config.link.includes('title')) {
const defaultLinkRender = md.renderer.rules.link_open;

// eslint-disable-next-line camelcase
md.renderer.rules.link_open = (tokens: Remarkable.LinkOpenToken[], idx: number, ...args: []) => {
let downloadAttr: boolean = false;
tokens.map((token: Remarkable.LinkOpenToken) => {
if (token) {
if (token.title === '' || token.title === 'download') {
const linkTextId = idx + 1;
if (config.download && token.title === 'download') {
downloadAttr = true;
if (token && token.title === '') {
const linkTextId = idx + 1;
if (tokens[linkTextId]) {
const linkContent: Remarkable.ContentToken = tokens[linkTextId];
if (linkContent.type === 'text') {
token.title = linkContent.content;
}
}
}
});

return defaultLinkRender(tokens, idx, ...args);
};
}

if (config.download) {
const defaultLinkRender = md.renderer.rules.link_open;

// eslint-disable-next-line camelcase
md.renderer.rules.link_open = (tokens: Remarkable.LinkOpenToken[], idx: number, ...args: []) => {
let downloadAttr = false;
tokens.map((token: Remarkable.LinkOpenToken) => {
if (token) {
if (token.title === 'download') {
const linkTextId = idx + 1;
downloadAttr = true;
if (tokens[linkTextId]) {
const linkContent: Remarkable.ContentToken = tokens[linkTextId];
if (linkContent.type === 'text') {
Expand Down

0 comments on commit 9d6554a

Please sign in to comment.