-
Notifications
You must be signed in to change notification settings - Fork 97
/
index.es
161 lines (147 loc) · 5.3 KB
/
index.es
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* hexo-tag-aplayer
* https://github.com/grzhan/hexo-tag-aplayer
* Copyright (c) 2016, grzhan
* Licensed under the MIT license.
*
* Syntax:
* {% aplayer title author url [picture_url, narrow, autoplay] %}
*/
require('babel-polyfill')
import fs from 'hexo-fs'
import {throwError} from "./common/util"
import util from 'hexo-util'
import {
APLAYER_SCRIPT_MARKER, APLAYER_TAG_MARKER, APLAYER_SECONDARY_SCRIPT_MARKER,
METING_TAG_MARKER, METING_SCRIPT_MARKER, METING_SECONDARY_SCRIPT_MARKER, APLAYER_SECONDARY_STYLE_MARKER,
APLAYER_STYLE_MARKER
} from './common/constant'
import MetingTag from "./lib/tag/playerMeting"
import APlayerTag from './lib/tag/player'
import APlayerLyricTag from './lib/tag/playerLyric'
import APlayerListTag from './lib/tag/playerList'
import PartialView from './lib/view'
import Config from './lib/config'
const log = require('hexo-log')({name: 'hexo-tag-aplayer', debug: false})
const config = new Config(hexo)
const APLAYER_STYLE_LITERAL = `<link rel="stylesheet" class="${APLAYER_SECONDARY_STYLE_MARKER}" href="${config.get('style')}">`
const APLAYER_SCRIPT_LITERAL = `<script src="${config.get('script')}" class="${APLAYER_SECONDARY_SCRIPT_MARKER}"></script>`
const METING_SCRIPT_LITERAL = config.get('meting_api')
? `<script>var meting_api='${config.get('meting_api')}?server=:server&type=:type&id=:id&r=:r'</script><script class="${METING_SECONDARY_SCRIPT_MARKER}" src="${config.get('meting_script')}"></script>`
: `<script class="${METING_SECONDARY_SCRIPT_MARKER}" src="${config.get('meting_script')}"></script>`
let filterEmitted = {after_render: false, after_post_render: false}
config.get('assets').forEach(asset => {
const [external, name, dstPath, srcPath] = asset
if (!external && config.get('asset_inject') && fs.existsSync(srcPath)) {
hexo.extend.generator.register(name, () => {
return {
path: dstPath,
data() {
return fs.createReadStream(srcPath)
}
}
})
}
})
hexo.extend.filter.register('after_render:html', function(raw, info) {
filterEmitted.after_render = true
if (!config.get('asset_inject')) {
return
}
const view = new PartialView(raw, info)
if (view.isFullPage()) {
if (!view.hasHeadTag()) {
log.warn(`[hexo-tag-aplayer]: <head> not found in ${view.path}, unable to inject script (like 'APlayer.js') in this page.`)
return
}
// Inject APlayer script
if (view.hasTagMarker(APLAYER_TAG_MARKER) && !view.assetAlreadyInjected(APLAYER_SCRIPT_MARKER)) {
view.injectAsset(`<link rel="stylesheet" href="${config.get('style')}" class="${APLAYER_STYLE_MARKER}">`)
view.injectAsset(util.htmlTag('script', {src: config.get('script'), class: APLAYER_SCRIPT_MARKER}, ''))
}
// Inject Meting script
if (config.get('meting') && view.hasTagMarker(METING_TAG_MARKER) && !view.assetAlreadyInjected(METING_SCRIPT_MARKER)) {
if (config.get('meting_api')) {
view.injectAsset( `<script>var meting_api='${config.get('meting_api')}?server=:server&type=:type&id=:id&r=:r'</script>`)
}
view.injectAsset(util.htmlTag('script', {src: config.get('meting_script'), class: METING_SCRIPT_MARKER}, ''))
}
// Remove duplicate scripts
view.removeLiteral(APLAYER_SCRIPT_LITERAL)
view.removeLiteral(METING_SCRIPT_LITERAL)
view.removeLiteral(APLAYER_STYLE_LITERAL)
}
return view.content
})
hexo.extend.filter.register('after_post_render', (data) => {
filterEmitted.after_post_render = true
if (!config.get('asset_inject')) {
return
}
// Polyfill: filter 'after_render:html' may not be fired in some cases, see https://github.com/hexojs/hexo-inject/issues/1
if (config.get('meting')) {
data.content = METING_SCRIPT_LITERAL + data.content
}
data.content = APLAYER_STYLE_LITERAL + APLAYER_SCRIPT_LITERAL + data.content
return data
})
hexo.extend.tag.register('aplayer', function(args) {
try {
const tag = new APlayerTag(hexo, args, this._id)
const output = tag.generate()
return output
} catch (e) {
console.error(e);
return `
<script>
console.error("${e}");
</script>`;
}
})
hexo.extend.tag.register('aplayerlrc', function(args, content) {
try {
const tag = new APlayerLyricTag(hexo, args, this._id, content)
const output = tag.generate()
return output
} catch (e) {
console.error(e);
return `
<script>
console.error("${e}");
</script>`
}
}, {ends: true})
hexo.extend.tag.register('aplayerlist', function(args, content) {
try {
const tag = new APlayerListTag(hexo, content, this._id)
const output = tag.generate()
return output
} catch (e) {
console.error(e)
return `
<script>
console.error("${e}");
</script>`
}
}, {ends: true})
hexo.extend.tag.register('meting', function(args) {
try {
if (!config.get('meting')) {
throwError('Meting support is disabled, cannot resolve the meting tags properly.')
}
const tag = new MetingTag(hexo, args, this._id)
const output = tag.generate()
return output
} catch (e) {
console.error(e)
return `
<script>
console.error("${e}");
</script>`
}
})
hexo.extend.tag.register('before_exit', function() {
if (!filterEmitted.after_render && filterEmitted.after_post_render) {
log.warn('Filter "after_render:html" not emitted during this generation, duplicate scripts would not be removed.')
}
})