-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (64 loc) · 2.32 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
69
70
71
72
73
74
75
76
77
78
79
80
81
'use strict'
const EMBED_REGEX = /@S\[([a-zA-Z].+)\]\([\s]*(.*?)[\s]*[\)]/im
function podcast_embed(md, options) {
function podcast_return(state, silent) {
var token;
var serviceEnd;
var serviceStart;
var oldPos = state.pos;
if (state.src.charCodeAt(oldPos) !== 0x40/* @ */ ||
state.src.charCodeAt(oldPos + 1) !== 0x53/* S */) {
return false;
}
var match = EMBED_REGEX.exec(state.src);
if (!match || match.length < 3) {
return false;
}
serviceStart = oldPos + 3;
serviceEnd = md.helpers.parseLinkLabel(state, oldPos + 1, false);
if (!silent) {
state.pos = serviceStart;
state.posMax = serviceEnd;
state.service = state.src.slice(serviceStart, serviceEnd);
var newState = new state.md.inline.State('soundcloud', state.md, state.env, []);
newState.md.inline.tokenize(newState);
token = state.push('podcast', '');
token.podcastUrl = match[2];
token.service = 'soundcloud';
token.level = state.level;
}
state.pos = state.pos + state.src.indexOf(')', state.pos);
state.posMax = state.tokens.length;
return true;
}
return podcast_return;
}
function tokenize_podcast(md, options) {
function tolenize_podcast_return(token, idx) {
var podcastUrl = md.utils.escapeHtml(token[idx].podcastUrl);
options.url = podcastUrl;
return podcastUrl === '' ? '' :
'<div><iframe width="100%" height="' + options.height + '" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?url=' + options.url + '&auto_play=' + options.auto_play + '&hide_related=' + options.hide_related + '&show_comments=' + options.show_comments + '&show_user=' + options.show_user + '&show_reposts=' + options.show_reposts + '&visual=' + options.visual + '"></iframe></div>';
}
return tolenize_podcast_return;
}
var defaults = {
url: '',
height: 450,
auto_play: false,
hide_related: false,
show_comments: true,
show_user: true,
show_reposts: false,
visual: true
};
module.exports = function podcast_plugin(md, options) {
if (typeof options === 'object') {
options = Object.assign({}, defaults, options);
}
else {
options = defaults;
}
md.renderer.rules.podcast = tokenize_podcast(md, options);
md.inline.ruler.before('emphasis', 'audio', podcast_embed(md, options));
}