diff options
author | Martin Fischer <martin@push-f.com> | 2021-12-10 10:36:00 +0100 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2021-12-10 10:36:00 +0100 |
commit | 950ab4cba9cc05b5446aaf889568cfc270d2139c (patch) | |
tree | c8a61ce97eaf95fd9c5e69f89ce4ad3de37e5fec | |
parent | 8be7aa457cc8907acccb75bf5efcdc26c5b952ee (diff) |
split lib from non-lib features
-rwxr-xr-x | build.py | 26 | ||||
-rw-r--r-- | script.js | 97 |
2 files changed, 85 insertions, 38 deletions
@@ -62,10 +62,26 @@ def get_features(dirname): features[key] = data - return dict( + features = dict( sorted(features.items(), key=lambda t: t[1]['title'].replace('`', '').lower()) ) + lib_features = {} + non_lib_features = {} + + for key, data in features.items(): + if ('flag' in data and data['flag'] in library_flags) or 'impl for' in data[ + 'title' + ]: + lib_features[key] = data + else: + non_lib_features[key] = data + + return dict(lib_features=lib_features, non_lib_features=non_lib_features) + + +with open('lib_feats.txt') as f: + library_flags = set([l.strip() for l in f]) with open('caniuse.rs/data/versions.toml') as f: versions = toml.load(f) @@ -91,6 +107,14 @@ with open('target/data.json', 'w') as f: def write_features(f, id, features): + if features['non_lib_features']: + write_feature_list(f, id + '-non-lib', features['non_lib_features']) + if features['lib_features']: + f.write('<h3 id="{}">library</h3>'.format(id + '-lib')) + write_feature_list(f, id + '-lib', features['lib_features']) + + +def write_feature_list(f, id, features): f.write('<ul id={}-list'.format(id)) if len(features) > 5: f.write(' class=columns') @@ -13,46 +13,69 @@ heading.hidden = query.length != 0; continue; } - const results = Object.values(data.features).filter( - feat => { - if (feat.title.toLowerCase().replaceAll('`', '').includes(query)) { - return true; - } - if (feat.flag && feat.flag.toLowerCase().includes(query)) { - return true; - } - if (query.length > 1) { - if (feat.aliases && feat.aliases.some(a => a.toLowerCase().includes(query))) { - return true; - } - if (feat.items && feat.items.some(i => i.toLowerCase().includes(query))) { - return true; - } - } - - return false; - } - ); + const nonLibResults = search(data.features.non_lib_features, query); + const libResults = search(data.features.lib_features, query); + + const totalResultCount = nonLibResults.length + libResults.length; // so that release notes don't get in the way when <Tab>ing through results - document.body.classList.toggle('hide-release-notes', results.length == 0); - - const ul = document.createElement('ul'); - for (const feat of results) { - const li = document.createElement('li'); - const a = document.createElement('a'); - a.textContent = feat.title; - a.innerHTML = a.innerHTML.replaceAll(/`(.+?)`/g, (_, x) => `<code>${x}</code>`); - a.href = feat.url; - if (feat.flag) - a.title = feat.flag; - li.appendChild(a); - ul.appendChild(li); + document.body.classList.toggle('hide-release-notes', totalResultCount == 0); + + let list = document.getElementById(key + '-non-lib-list'); + if (list) { + list.replaceChildren(...renderList(nonLibResults).children); + list.hidden = nonLibResults.length == 0; + } + + list = document.getElementById(key + '-lib-list'); + if (list) { + list.replaceChildren(...renderList(libResults).children); + list.hidden = libResults.length == 0; + document.getElementById(key + '-lib').hidden = libResults.length == 0; } - const list = document.getElementById(key + '-list'); - list.replaceChildren(...ul.children); - list.hidden = results.length == 0; - heading.hidden = results.length == 0; + + // TODO: hide library heading if no lib results + + heading.hidden = totalResultCount == 0; } }); })(); + +function search(features, query) { + return Object.values(features).filter( + feat => { + if (feat.title.toLowerCase().replaceAll('`', '').includes(query)) { + return true; + } + if (feat.flag && feat.flag.toLowerCase().includes(query)) { + return true; + } + if (query.length > 1) { + if (feat.aliases && feat.aliases.some(a => a.toLowerCase().includes(query))) { + return true; + } + if (feat.items && feat.items.some(i => i.toLowerCase().includes(query))) { + return true; + } + } + + return false; + } + ); +} + +function renderList(results) { + const ul = document.createElement('ul'); + for (const feat of results) { + const li = document.createElement('li'); + const a = document.createElement('a'); + a.textContent = feat.title; + a.innerHTML = a.innerHTML.replaceAll(/`(.+?)`/g, (_, x) => `<code>${x}</code>`); + a.href = feat.url; + if (feat.flag) + a.title = feat.flag; + li.appendChild(a); + ul.appendChild(li); + } + return ul; +} |