diff --git a/build.sh b/build.sh
index fb9d671d3..f5049da18 100755
--- a/build.sh
+++ b/build.sh
@@ -31,6 +31,9 @@ cp -RL www/* $CUSTOM_ASSETS $DEST/
# Assemble base CSS modules followed by flavor customizations
node scripts/assemble-css.js "$DEST/style.css" $CUSTOM_CSS
+# Compile the API docs into structure and per-locale message catalogs
+node scripts/build-api-docs.js docs/api.md docs/i18n $DEST
+
# Open search (requires absolute CANONICAL_URL)
if [ -n "$CANONICAL_URL" ]; then
pug client/opensearch.pug -E xml -o $DEST
diff --git a/client/src/app.js b/client/src/app.js
index 8d41aa75c..11b3d9017 100644
--- a/client/src/app.js
+++ b/client/src/app.js
@@ -10,6 +10,7 @@ import { isBitcoinNetwork, isBitcoinOrLiquidMainnet } from './lib/network'
import { normalizeSearchQuery } from './lib/search-query'
import { debounceAutocompleteQueries, getAutocompletePlan } from './lib/search-autocomplete'
import getPrivacyAnalysis from './lib/privacy-analysis'
+import { buildDocsSearchIndex, searchDocs } from './lib/api-docs-search'
import {
highValueAssetDefinitions,
blockTxsPerPage,
@@ -55,7 +56,11 @@ const highValueAssetCategory = assetId => `high-value-asset-${assetId}`
apiBase,
process.browser ? window.location.origin : process.env.CANONICAL_URL
)
- , setBase = ({ path, ...r }) => ({ ...r, url: path.includes('://') || path.startsWith('./') ? path : apiBase + path })
+ , staticRoot = process.env.STATIC_ROOT || ''
+ // The pre-renderer fetches from Node, which needs an absolute URL; its API_URL points at the same nginx
+ , docsAssetUrl = file => new URL(staticRoot + file, process.browser ? document.baseURI : apiBase).href
+ , docsTryBodyLimit = 20000
+ , setBase = ({ path, ...r }) => ({ ...r, url: path.includes('://') ? path : apiBase + path })
, highValueAssetRequests = !showHighValueAssets ? [] : highValueAssetDefinitions.reduce((requests, asset) => [
...requests,
{
@@ -74,7 +79,7 @@ const highValueAssetCategory = assetId => `high-value-asset-${assetId}`
}] : [])
], [])
-const reservedPaths = [ 'mempool', 'assets', 'search' ]
+const reservedPaths = [ 'mempool', 'assets', 'search', 'docs' ]
, recentAssetsCount = 10
, assetListQueryKeys = [ 'start_index', 'sort_field', 'sort_dir', 'limit' ]
, isAssetListQuery = query => assetListQueryKeys.some(key => query[key] != null)
@@ -192,6 +197,29 @@ const replaceQueryWithoutRouting = (page, search) => ({
state: { ...page.state, scrollKey: pageScrollKey(page) ?? null }
})
+const formatDocsTryBody = res => {
+ const text = res.body && typeof res.body == 'object' && !(res.body instanceof ArrayBuffer)
+ ? JSON.stringify(res.body, null, 2)
+ : res.text || ''
+ return text.length > docsTryBodyLimit ? `${text.slice(0, docsTryBodyLimit)}\n…` : text
+}
+
+const isDocsPath = pathname => /^\/docs(\/[^/]+\/[^/]+)?\/?$/.test(pathname)
+
+const docsPageInViewId = () => {
+ let id = null
+ for (const page of document.querySelectorAll('.docs-page')) {
+ if (page.getBoundingClientRect().top > 120) break
+ id = page.id
+ }
+ return id
+}
+
+const docsAnchorNav = (id, docs) => {
+ const [ path, hash ] = docs.anchors[id].split('#')
+ return { pathname: `/docs/${path}`, hash: hash ? `#${hash}` : '' }
+}
+
const isBitcoinUsdValuePage = pathname =>
pathname == '/' || pathname == '/tx/recent' || pathname == '/mempool'
|| /^\/(tx|block)\/[a-f0-9]{64}\/?$/i.test(pathname)
@@ -317,6 +345,8 @@ export default function main(
, goScan$ = route('/scan-qr').mapTo(true)
, goMempool$= route('/mempool')
, goSearch$ = route('/search').map(loc => loc.query.q).filter(Boolean)
+ , goDocs$ = O.merge(route('/docs'), route('/docs/:group/:section'))
+ .map(loc => ({ ...loc, sectionPath: loc.params.group ? `${loc.params.group}/${loc.params.section}` : null }))
// Elements only
, goAsset$ = !process.env.IS_ELEMENTS ? O.empty() : route('/asset/:asset_id').map(loc => ({
@@ -375,7 +405,8 @@ export default function main(
.map(e => e.detail && e.detail.txid)
.filter(isHash256)
- , copy$ = click('[data-clipboard-copy]').map(d => d.clipboardCopy)
+ , copy$ = O.merge(click('[data-clipboard-copy]').map(d => d.clipboardCopy)
+ , on('[data-clipboard-copy-input]', 'click').map(({ ownerTarget: btn }) => btn.form.elements[btn.dataset.clipboardCopyInput].value))
, pushtx$ = (process.browser
? on('form[data-do=pushtx]', 'submit', { preventDefault: true }).map(e => e.ownerTarget.querySelector('[name=tx]').value)
: goPush$.filter(loc => loc.body && loc.body.tx).map(loc => loc.body.tx)
@@ -387,6 +418,32 @@ export default function main(
, moreSTxs$ = click('[data-loadmore-txs-asset]').map(d => ({ asset_id: d.loadmoreTxsAsset, last_txid: d.loadmoreTxsLastTxid }))
+ , docsTier$ = storage.local.getItem('docsTier').first().map(tier => tier == 'enterprise' ? tier : 'free')
+ .concat(click('[data-docs-tier]').map(d => d.docsTier))
+ .distinctUntilChanged()
+ , docsCodeLang$ = click('[data-code-tab]').map(d => d.codeTab)
+ // The toggle carries the action matching what is shown: an active filter also keeps the panel open.
+ , docsNavToggle$ = click('[data-docs-nav-toggle]').map(d => d.docsNavToggle).share()
+ , docsNavOpen$ = O.merge(
+ docsNavToggle$.map(action => action == 'open')
+ , page$.mapTo(false)
+ )
+ , docsFilterInput$ = on('.docs-filter-input', 'input').map(e => e.ownerTarget.value)
+ , docsFilterKeydown$ = on('.docs-filter-input', 'keydown')
+ .filter(e => [ 'ArrowDown', 'ArrowUp', 'Enter', 'Escape' ].includes(e.key))
+ .share()
+ , docsTryRequest$ = on('form[data-docs-try]', 'submit', { preventDefault: true })
+ .map(e => {
+ const form = e.ownerTarget
+ , path = form.dataset.docsTryPath.replace(/:([a-z_]+)/g, (_, name) => encodeURIComponent(form.elements[name].value.trim()))
+ return {
+ pageId: form.dataset.docsTry,
+ path,
+ displayUrl: process.browser ? new URL(apiBase + path, document.baseURI).href : apiBase + path
+ }
+ })
+ .share()
+
, lang$ = storage.local.getItem('lang').first().map(lang => lang || defaultLang)
.concat(on('select[name=lang]', 'input').map(e => e.target.value))
.distinctUntilChanged()
@@ -694,9 +751,55 @@ export default function main(
// In elements, we block rendering until the assetMap is loaded. Otherwise, we can start immediately.
, isReady$ = process.env.ASSET_MAP_URL ? assetMap$.mapTo(true).startWith(false) : O.of(true)
+ // API docs: locale-independent structure plus a message catalog for the current language
+ , docs$ = reply('api-docs').shareReplay(1)
+ , docsError$ = extractErrors(O.merge(HTTP.select('api-docs'), HTTP.select('api-docs-messages')))
+ , docsLocale$ = docs$.combineLatest(lang$, (docs, lang) => docs.locales.includes(lang) ? lang : 'en')
+ .distinctUntilChanged()
+ , docsMessages$ = reply('api-docs-messages')
+ , docsFilter$ = O.merge(
+ docsFilterInput$
+ , docsFilterKeydown$.filter(e => e.key == 'Escape').mapTo('')
+ , docsNavToggle$.filter(action => action == 'close').mapTo('')
+ ).startWith('')
+ , docsSearchIndex$ = O.combineLatest(docs$, docsMessages$, buildDocsSearchIndex).shareReplay(1)
+ , docsHits$ = docsSearchIndex$
+ .combineLatest(docsTier$, (index, tier) => tier == 'enterprise' ? index : index.filter(entry => !entry.enterpriseOnly))
+ .combineLatest(docsFilter$, searchDocs)
+ // Starts on the best match; arrow keys then move through hits in sidebar order.
+ , docsActiveHit$ = docsHits$.withLatestFrom(docsSearchIndex$).switchMap(([ hits, index ]) => {
+ if (!hits || !hits.length) return O.of(null)
+ const ordered = index.map(entry => entry.id).filter(id => hits.includes(id))
+ return docsFilterKeydown$
+ .filter(e => e.key == 'ArrowDown' || e.key == 'ArrowUp')
+ .scan((id, e) => {
+ e.preventDefault()
+ return ordered[(ordered.indexOf(id) + (e.key == 'ArrowDown' ? 1 : -1) + ordered.length) % ordered.length]
+ }, hits[0])
+ .startWith(hits[0])
+ })
+ , docsSelectHit$ = docsFilterKeydown$.filter(e => e.key == 'Enter')
+ .withLatestFrom(docsActiveHit$, docs$, (_, id, docs) => id && docsAnchorNav(id, docs))
+ .filter(Boolean)
+ , docsActiveId$ = goDocs$.map(loc => (loc.hash || '').replace(/^#/, '') || null)
+ , docsSectionPath$ = goDocs$.map(loc => loc.sectionPath)
+ // Links from before docs were split into sections (/docs#id) open the section holding the anchor
+ , docsLegacyAnchor$ = goDocs$.filter(loc => !loc.sectionPath && loc.hash)
+ .switchMap(loc => docs$.first().map(docs => Object.prototype.hasOwnProperty.call(docs.anchors, loc.hash.slice(1)) && docsAnchorNav(loc.hash.slice(1), docs)))
+ .filter(Boolean)
+ , docsTry$ = O.merge(
+ docsTryRequest$.map(req => S => ({ ...S, [req.pageId]: { loading: true, url: req.displayUrl } }))
+ , HTTP.select('docs-try').flatMap(r$ => r$
+ .map(res => ({ status: res.status, body: formatDocsTryBody(res) }))
+ .catch(err => O.of({ status: err.status, body: err.response ? formatDocsTryBody(err.response) : err.message }))
+ .map(result => S => ({ ...S, [r$.request.pageId]: { ...result, url: r$.request.displayUrl } })))
+ ).startWith({}).scan((S, mod) => mod(S))
+
// Currently visible view
- , view$ = O.merge(page$.mapTo(null)
+ // Docs navigations keep the docs view mounted so moving between anchors doesn't rebuild the page
+ , view$ = O.merge(page$.filter(loc => !isDocsPath(loc.pathname)).mapTo(null)
, goHome$.mapTo('dashBoard')
+ , goDocs$.mapTo('docs')
, goAPILanding$.mapTo('apiLanding')
, goBlocks$.mapTo('recentBlocks')
, goRecent$.mapTo('recentTxs')
@@ -713,6 +816,13 @@ export default function main(
.combineLatest(isReady$, loading$, (view, isReady, loading) =>
!isReady ? 'loading' : view || (loading ? 'loading' : 'notFound'))
+ // The docs page currently scrolled to, for the section bar and sidebar highlight
+ , docsScrollId$ = !process.browser ? O.empty() : O.merge(
+ goDocs$.mapTo(null)
+ , O.fromEvent(window, 'scroll').throttleTime(100, undefined, { leading: true, trailing: true })
+ .withLatestFrom(view$).filter(([ _, view ]) => view == 'docs').map(docsPageInViewId)
+ ).distinctUntilChanged()
+
, dashboardLatestBlock$ = isBitcoinNetwork
? latestBlock$
.combineLatest(view$, (block, view) => view == 'dashBoard' ? block : null)
@@ -771,6 +881,7 @@ export default function main(
// Page title
, title$ = O.merge(page$.mapTo(null)
, goAPILanding$.withLatestFrom(t$, (_, t) => t`Explorer API`)
+ , goDocs$.withLatestFrom(t$, (_, t) => t`API Documentation`)
, block$.filter(notNully).withLatestFrom(t$, (block, t) => t`Block #${block.height}: ${block.id}`)
, tx$.filter(notNully).withLatestFrom(t$, (tx, t) => t`Transaction: ${tx.txid}`)
, addr$.filter(notNully).withLatestFrom(goAddr$, t$, (_, goAddr, t) => t`Address: ${goAddr.display_addr}`)
@@ -793,6 +904,7 @@ export default function main(
, goAddr$, addr$, addrTxs$, addrQR$
, assetMap$, assetList$, goAssetList$, recentAssets$, highValueAssets$, goAsset$, asset$, assetTxs$, unblinded$
, autocomplete$: autocomplete$.distinctUntilChanged(sameAutocompleteView)
+ , docs$, docsError$, docsMessages$, docsTier$, docsCodeLang$, docsFilter$, docsHits$, docsActiveHit$, docsActiveId$, docsSectionPath$, docsScrollId$, docsNavOpen$, docsTry$
, isReady$, loading$, page$, view$, title$
})
@@ -968,6 +1080,17 @@ export default function main(
// fetch more txs for asset page
, moreSTxs$.map(d => ({ category: 'asset-txs-more', method: 'GET', path: `/asset/${d.asset_id}/txs/chain/${d.last_txid}` }))
+
+ // API docs are static assets, fetched on first entry and kept for the session
+ , goDocs$.withLatestFrom(docs$.startWith(null)).filter(([ _, docs ]) => !docs)
+ .map(_ => ({ category: 'api-docs', method: 'GET', path: docsAssetUrl('api-docs.json'), bg: !!process.browser }))
+ // The pre-renderer fetches the English catalog alongside the structure, so its loading count
+ // can't reach zero between the two requests and finish the render early
+ , (process.browser ? docsLocale$ : goDocs$.mapTo('en'))
+ .map(locale => ({ category: 'api-docs-messages', method: 'GET', path: docsAssetUrl(`api-docs.${locale}.json`), bg: !!process.browser }))
+
+ // Try Code: one user-initiated request per click, never retried or polled
+ , docsTryRequest$.map(req => ({ category: 'docs-try', method: 'GET', path: req.path, pageId: req.pageId, displayUrl: req.displayUrl, bg: true }))
).map(setBase)
// DOM sink
@@ -976,6 +1099,7 @@ export default function main(
// localStorage sink
, store$ = O.merge(
lang$.skip(1).map(lang => ({ key: 'lang', value: lang }))
+ , docsTier$.skip(1).map(tier => ({ key: 'docsTier', value: tier }))
)
// Route navigation sink
@@ -993,6 +1117,8 @@ export default function main(
.map(page => replaceQueryWithoutRouting(page,
updateQuery(page.query, { 'pending-block': pendingBlockDetailsAreOpen(page) ? 'collapsed' : false })))
, searchQuery$.map(q => ({ type: 'push', pathname: '/search', search: `q=${encodeURIComponent(dropBlindingFragment(q))}` }))
+ , docsSelectHit$.map(loc => ({ type: 'push', ...loc }))
+ , docsLegacyAnchor$.map(loc => ({ type: 'replace', ...loc }))
)
dbg({ goBlocks$, goBlock$, goTx$, goAddr$, togTx$, page$, lang$, vdom$, moreBlocks$
@@ -1027,8 +1153,8 @@ export default function main(
})
// Reset scrolling when navigating to a new page (but not when hitting 'back')
- page$.startWith([ ]).scan((prevKeys, loc) => [ ...prevKeys.slice(0, 15), pageScrollKey(loc) ?? null ])
- .filter(keys => keys.length && !keys.slice(0, -1).includes(last(keys)))
+ page$.scan(({ keys }, loc) => ({ loc, keys: [ ...keys.slice(0, 15), pageScrollKey(loc) ?? null ] }), { keys: [] })
+ .filter(({ loc, keys }) => !keys.slice(0, -1).includes(last(keys)) && !(isDocsPath(loc.pathname) && loc.hash))
.subscribe(_ => window.scrollTo(0, 0))
// Scroll ins/outs selected via URL hash into view (single tx page only)
@@ -1039,8 +1165,33 @@ export default function main(
.distinctUntilChanged().delay(300)
.subscribe(el => el.scrollIntoView({ behavior: 'smooth' }))
+ const revealActiveDocsNavItem = () => {
+ const nav = document.querySelector('.docs-nav')
+ , item = nav && (nav.querySelector('.docs-nav-link.active') || nav.querySelector('summary.active'))
+ if (!item) return
+ const navBox = nav.getBoundingClientRect()
+ , itemBox = item.getBoundingClientRect()
+ if (itemBox.top < navBox.top || itemBox.bottom > navBox.bottom)
+ nav.scrollTop += itemBox.top - navBox.top - (nav.clientHeight - itemBox.height) / 2
+ }
+
+ click('[data-docs-nav-toggle]')
+ .switchMap(_ => DOM.select('.docs-sidebar.nav-open').elements().filter(els => els.length).first())
+ .subscribe(revealActiveDocsNavItem)
+
+ // Scrolls in the same render that shows the section, so no frame paints at the old position
+ goDocs$.filter(loc => loc.sectionPath)
+ .switchMap(loc => DOM.select(`.docs-content[data-docs-section="${loc.sectionPath}"]`).elements()
+ .filter(els => els.length && (!loc.hash || document.getElementById(loc.hash.slice(1))))
+ .first().mapTo(loc))
+ .subscribe(loc => {
+ const target = loc.hash && document.getElementById(loc.hash.slice(1))
+ target && target.scrollIntoView()
+ revealActiveDocsNavItem()
+ })
+
// Display "Copied!" tooltip
- on('[data-clipboard-copy]', 'click').subscribe(({ ownerTarget: btn }) => {
+ on('[data-clipboard-copy], [data-clipboard-copy-input]', 'click').subscribe(({ ownerTarget: btn }) => {
btn.classList.add('show-tooltip')
setTimeout(_ => btn.classList.remove('show-tooltip'), 700)
})
@@ -1119,7 +1270,7 @@ export default function main(
, noExtraModifiers = !e.altKey && !e.shiftKey
if (hasModifier && noExtraModifiers && e.key.toLowerCase() == 'k') {
- const searchInput = document.querySelector('.search-bar-input')
+ const searchInput = document.querySelector('.docs-filter-input') || document.querySelector('.search-bar-input')
if (searchInput) {
e.preventDefault()
searchInput.focus()
diff --git a/client/src/components/code-block.js b/client/src/components/code-block.js
new file mode 100644
index 000000000..bcca385bc
--- /dev/null
+++ b/client/src/components/code-block.js
@@ -0,0 +1,38 @@
+import { CopyIcon } from './icons'
+
+// Fallback for code without build-time highlighting (e.g. live responses):
+// double-quoted strings are highlighted and JSON object keys are left plain.
+const stringToken = /"(?:[^"\\\n]|\\.)*"(\s*:)?/g
+
+const highlightStrings = code => {
+ const parts = []
+ let last = 0, match
+ while ((match = stringToken.exec(code))) {
+ if (match[1]) continue
+ parts.push(code.slice(last, match.index), {match[0]})
+ last = match.index + match[0].length
+ }
+ parts.push(code.slice(last))
+ return parts
+}
+
+const entities = { '&': '&', '<': '<', '>': '>', '"': '"', ''': "'", ''': "'" }
+
+const htmlText = html => html.replace(/<[^>]+>/g, '').replace(/&(amp|lt|gt|quot|#x27|#39);/g, entity => entities[entity])
+
+// Pass either plain `code` or pre-highlighted `html`.
+export const CodeBlock = ({ code, html, title, meta, copyLabel = 'Copy code' }) =>
+
+ { title || meta ?
+ { title ?
{title}
: '' }
+ { meta ? {meta} : '' }
+ : '' }
+
+ { html
+ ?
+ :
{highlightStrings(code)}
}
+
+
+
diff --git a/client/src/components/code-tabs.js b/client/src/components/code-tabs.js
new file mode 100644
index 000000000..a33bc5c08
--- /dev/null
+++ b/client/src/components/code-tabs.js
@@ -0,0 +1,23 @@
+import { CodeBlock } from './code-block'
+
+// items: [{ label, code or html }]. Selecting a tab emits its label via data-code-tab.
+// children render below the code, inside the same box.
+export const CodeTabs = ({ items, selected, title, meta, copyLabel, children = [] }) => {
+ const active = items.find(item => item.label == selected) || items[0]
+ return
+
+
+ { items.map(item =>
+ ) }
+
+ { meta ?
{meta} : '' }
+
+
+ { CodeBlock({ code: active.code, html: active.html, title, copyLabel }) }
+ { children }
+
+
+}
diff --git a/client/src/components/icons.js b/client/src/components/icons.js
index 22883416f..a6a95d737 100644
--- a/client/src/components/icons.js
+++ b/client/src/components/icons.js
@@ -31,6 +31,11 @@ export const LinkIcon = ({ className } = {}) =>
+export const InfoIcon = ({ className } = {}) =>
+
+
export const CopyIcon = ({ className } = {}) =>