-
Notifications
You must be signed in to change notification settings - Fork 6
/
kunai.js
196 lines (158 loc) · 6.11 KB
/
kunai.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import {KunaiError} from './kunai/error'
import {Compat} from './kunai/compat'
import * as UI from './kunai/ui'
import {Meta} from './kunai/meta'
import {Wand} from './kunai/wand'
import * as Mirror from './kunai/mirror'
import {Yata} from './kunai/yata'
import './codemirror-themes'
import {CRSearch} from 'crsearch'
class DefaultLogger {
constructor(context = "kunai_top") {
this.context = context;
}
debug() { console.debug(`[${this.context}]`, ...arguments); }
info() { console.info(`[${this.context}]`, ...arguments); }
warn() { console.warn(`[${this.context}]`, ...arguments); }
error() { console.error(`[${this.context}]`, ...arguments); }
makeContext(context) { return new DefaultLogger(context); }
};
class ErrorLogger {
constructor(context = "kunai_top") {
this.context = context;
}
debug() {}
info() {}
warn() {}
error() { console.error(`[${this.context}]`, ...arguments); }
makeContext(context) { return new ErrorLogger(context); }
};
class Kunai {
static defaultOptions = {
compat: true,
}
constructor(opts = {}) {
// fastest
// $('body').addClass('js').removeClass('no-js')
this.opts = Object.assign({}, Kunai.defaultOptions, opts)
//this.log = new DefaultLogger()
this.log = new ErrorLogger()
console.log(`version ${KUNAI_PACKAGE.version} (https://github.com/cpprefjp/kunai/tree/v${KUNAI_PACKAGE.version})`)
console.log(`please report frontend bugs to: ${KUNAI_PACKAGE.bugs_url}`)
this.ui = {
navbar: null,
sidebar: null,
content: null,
}
this.initUI()
this.wand = new Wand(this.log)
this.yatas = new Map
}
async cpprefjp() {
this.load_impl(['cpprefjp', 'site'])
this.crs = this.initCRSearch(true)
}
async boostjp() {
this.load_impl(['boostjp', 'site'])
this.crs = this.initCRSearch(false)
}
load_impl(config) {
if (this.opts.compat) {
this.compat = new Compat(this.log, config)
}
const desc = config.join('/')
this.log.info(`loading (${desc})`)
$('body').addClass('kunai')
const mdinfo = JSON.parse($('header').attr('data-kunai-mdinfo'))
this.log.info(`data-kunai-mdinfo`, mdinfo)
this.meta = new Meta(this.log, config, mdinfo, ::this.onCodeFound)
$('.yata > .codehilite').addClass('kunai-code')
this.log.info(`loaded (${desc})`)
}
onCodeFound(meta, id) {
this.yatas.set(id, new Yata(this.log, this.wand, meta.getCode(id)))
}
async initSidebar() {
this.ui.sidebar = new UI.Sidebar(this.log)
return this.ui.sidebar
}
async initUI() {
this.ui.navbar = new UI.Navbar(this.log)
this.ui.content = new UI.Content(this.log)
}
async onDatabase(db) {
// this.log.debug(`onDatabase`, db)
UI.Badge.onDatabase(db)
await this.ui.sidebar.onDatabase(db)
await this.ui.sidebar.treeview.onPageID(this.meta.page_id)
}
async initCRSearch(isEnabled) {
if (!isEnabled) return null
await this.initSidebar()
// Dynamically set the base_url
const dynamic_base_url = (() => {
// Determine the location of the website base
const current_script = document.currentScript || document.querySelector('script[src*="static/kunai/js/kunai.js"]')
if (current_script) {
// Try to determine the base_url based on the location of this script
// ({base_url}/static/kunai/js/kunai.js).
const url_kunai = current_script.getAttribute("src")
const url = url_kunai.replace(/\bstatic\/kunai\/js\/kunai\.js([?#].*)?$/, "")
if (url != url_kunai) return url == "" ? "/" : url
}
// Fallback case assuming that the website is hosted at the top level
return "/"
})()
// Determine the project website URL, which is assumed to be stored in
// <meta name="twietter:url" content="..." /> or in <meta property="og:url"
// content="..." />.
const online_base_url = (() => {
const meta = document.querySelector('meta[name="twitter:url"]') || document.querySelector('meta[property="og:url"]')
if (meta && meta.content) {
const m = meta.content.toString().match(/^https?:\/\/[^/]*\//)
if (m) return m[0]
}
return null
})()
const database_url = (() => {
// Determine the location of the database file "crsearch.json".
const current_script = document.currentScript || document.querySelector('script[src*="kunai/js/kunai.js"]')
if (current_script) {
// A special care is needed for local HTML files (file://...). When a
// HTML in a local file system is directly opened in a Web browser,
// "static/crsearch/crsearch.json" cannot be read using XHR due to the
// CORS (cross-origin resource sharing) policy for the local files.
if (/^file:\/\//.test(current_script.src)) {
const url_kunai = current_script.getAttribute("src")
// When the current script file (kunai.js) is located in an expected
// path in the tree, we try to load the local database file
// "crsearch/crsearch.js" in JSONP format.
const url = url_kunai.replace(/\bkunai\/js\/kunai\.js([?#].*)?$/, "crsearch/crsearch.js")
if (url != url_kunai) return url
// Try to download "crsearch.json" from the project website.
if (online_base_url)
return online_base_url + "static/crsearch/crsearch.json"
}
// Try to determine the position of crsearch.json
// ({base_url}/static/crsearch/crsearch.json) based on the location of
// this script ({base_url}/static/kunai/js/kunai.js).
const url_kunai = current_script.getAttribute("src")
const url = url_kunai.replace(/\bkunai\/js\/kunai\.js([?#].*)?$/, "crsearch/crsearch.json")
if (url != url_kunai) return url
}
// Fallback case assuming that the website is hosted at the top level
return "/static/crsearch/crsearch.json"
})();
let crs = new CRSearch({
onDatabase: this.onDatabase.bind(this),
base_url: dynamic_base_url,
online_base_url: online_base_url
})
crs.database(database_url)
let e = $('.crsearch')
await crs.searchbox(e)
e.addClass('loaded')
return crs
}
}
export {Kunai}