-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (68 loc) · 2.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const remarkableSeo = (md, options) => {
const defaultOptions = {
'download': true,
'image': [
'title'
],
'link': [
'title'
]
};
const config = Object.assign({}, defaultOptions, options);
if (config.image && config.image.includes('title')) {
const defaultImageRender = md.renderer.rules.image;
md.renderer.rules.image = (tokens, idx, ...args) => {
tokens.map((token) => {
if (token && token.alt !== '' && token.title === '') {
token.title = token.alt;
}
});
return defaultImageRender(tokens, idx, ...args);
};
}
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 === 'download') {
const linkTextId = idx + 1;
downloadAttr = true;
if (tokens[linkTextId]) {
const linkContent = tokens[linkTextId];
if (linkContent.type === 'text') {
token.title = linkContent.content;
}
}
}
}
});
let result = defaultLinkRender(tokens, idx, ...args);
if (downloadAttr) {
result = result.replace('>', ' download>');
}
return result;
};
}
};
module.exports = remarkableSeo;