From 31c97ce0f067ddf5c01217cf172ef1ff260ca564 Mon Sep 17 00:00:00 2001 From: asrar Date: Wed, 24 Jun 2020 05:12:40 +0530 Subject: Adds query param for version no (#4270) * Adds query param for version no This adds support for using a query parameter for selecting the version no * Adds error handling to configuration request Catch request exception in case fetching the configuration from the url fails, this can happen either if non existent version number is passed in or because of server issues. * Makes version selection better Covers a few common cases in which the version number can be specified. --- docs/index.html | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'docs') diff --git a/docs/index.html b/docs/index.html index c89c73d4cf7..c221c6db71f 100644 --- a/docs/index.html +++ b/docs/index.html @@ -82,6 +82,13 @@ const queryParams = new URLSearchParams(window.location.search); const searchParam = queryParams.get('search'); const searchTerm = null !== searchParam ? searchParam : ''; + const versionParam = queryParams.get('version'); + const parseVersionParam = (version) => { + if (version === 'master') return 'master'; + if (version.startsWith('v')) return version; + return `v${version}`; + }; + const versionNumber = null !== versionParam ? parseVersionParam(versionParam) : 'master'; new Vue({ el: '#app', data: { @@ -90,7 +97,7 @@ configurationDescriptions: [], searchCondition: searchTerm, shouldStable: false, - version: 'master', + version: versionNumber, oldVersion: undefined, versionOptions: ['master'] }, @@ -99,16 +106,20 @@ if (this.version !== this.oldVersion) { const ConfigurationMdUrl = `https://raw.githubusercontent.com/rust-lang/rustfmt/${this.version}/Configurations.md`; - const res = await axios.get(ConfigurationMdUrl); - const { - about, - configurationAbout, - configurationDescriptions - } = parseMarkdownAst(res.data); - this.aboutHtml = marked.parser(about); - this.configurationAboutHtml = marked.parser(configurationAbout); - this.configurationDescriptions = configurationDescriptions; - this.oldVersion = this.version; + try { + const res = await axios.get(ConfigurationMdUrl); + const { + about, + configurationAbout, + configurationDescriptions + } = parseMarkdownAst(res.data); + this.aboutHtml = marked.parser(about); + this.configurationAboutHtml = marked.parser(configurationAbout); + this.configurationDescriptions = configurationDescriptions; + this.oldVersion = this.version; + } catch(error) { + this.aboutHtml = "

Failed to get configuration options for this version, please select the version from the dropdown above.

"; + } } const ast = this.configurationDescriptions -- cgit 1.4.1-3-g733a5