|
5 | 5 | <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/3.0.1/github-markdown.css" /> |
6 | 6 | <script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script> |
7 | 7 | <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js"></script> |
| 8 | + <script src="https://unpkg.com/vue-async-computed@3.8.1"></script> |
8 | 9 | <script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script> |
9 | 10 | <style> |
10 | 11 | @media (max-width: 767px) { |
|
59 | 60 | <label for="stable">stable: </label> |
60 | 61 | <input type="checkbox" id="stable" v-model="shouldStable"> |
61 | 62 | </div> |
| 63 | + <div> |
| 64 | + <label for="version">version: </label> |
| 65 | + <select name="version" id="version" v-model="version"> |
| 66 | + <option v-for="option in versionOptions" v-bind:value="option"> |
| 67 | + {{ option }} |
| 68 | + </option> |
| 69 | + </select> |
| 70 | + </div> |
62 | 71 | </div> |
63 | 72 | <div v-html="aboutHtml"></div> |
64 | 73 | <div v-html="configurationAboutHtml"></div> |
65 | 74 | <div v-html="outputHtml"></div> |
66 | 75 | </article> |
67 | 76 | </div> |
68 | 77 | <script> |
69 | | - const ConfigurationMdUrl = 'https://raw.githubusercontent.com/rust-lang/rustfmt/master/Configurations.md'; |
| 78 | + const MajorVersionBounds = {min: 1, max: 2}; |
| 79 | + const RusfmtTagsUrl = 'https://api.github.com/repos/rust-lang/rustfmt/tags'; |
70 | 80 | const UrlHash = window.location.hash.replace(/^#/, ''); |
71 | 81 | new Vue({ |
72 | 82 | el: '#app', |
73 | | - data() { |
74 | | - const configurationDescriptions = []; |
75 | | - configurationDescriptions.links = {}; |
76 | | - return { |
77 | | - aboutHtml: '', |
78 | | - configurationAboutHtml: '', |
79 | | - searchCondition: UrlHash, |
80 | | - configurationDescriptions, |
81 | | - shouldStable: false |
82 | | - } |
| 83 | + data: { |
| 84 | + aboutHtml: '', |
| 85 | + configurationAboutHtml: '', |
| 86 | + configurationDescriptions: [], |
| 87 | + searchCondition: UrlHash, |
| 88 | + shouldStable: false, |
| 89 | + version: 'master', |
| 90 | + oldVersion: undefined, |
| 91 | + versionOptions: ['master'] |
83 | 92 | }, |
84 | | - computed: { |
85 | | - outputHtml() { |
| 93 | + asyncComputed: { |
| 94 | + async outputHtml() { |
| 95 | + if (this.version !== this.oldVersion) { |
| 96 | + const ConfigurationMdUrl = |
| 97 | + `https://raw.githubusercontent.com/rust-lang/rustfmt/${this.version}/Configurations.md`; |
| 98 | + const res = await axios.get(ConfigurationMdUrl); |
| 99 | + const { |
| 100 | + about, |
| 101 | + configurationAbout, |
| 102 | + configurationDescriptions |
| 103 | + } = parseMarkdownAst(res.data); |
| 104 | + this.aboutHtml = marked.parser(about); |
| 105 | + this.configurationAboutHtml = marked.parser(configurationAbout); |
| 106 | + this.configurationDescriptions = configurationDescriptions; |
| 107 | + this.oldVersion = this.version; |
| 108 | + } |
| 109 | + |
86 | 110 | const ast = this.configurationDescriptions |
87 | | - .filter(({ head, text, stable }) => { |
88 | | - |
89 | | - if ( |
90 | | - text.includes(this.searchCondition) === false && |
91 | | - head.includes(this.searchCondition) === false |
92 | | - ) { |
93 | | - return false; |
94 | | - } |
95 | | - return (this.shouldStable) |
96 | | - ? stable === true |
97 | | - : true; |
98 | | - }) |
99 | | - .reduce((stack, { value }) => { |
100 | | - return stack.concat(value); |
101 | | - }, []); |
| 111 | + .filter(({ head, text, stable }) => { |
| 112 | + if (text.includes(this.searchCondition) === false && |
| 113 | + head.includes(this.searchCondition) === false) { |
| 114 | + return false; |
| 115 | + } |
| 116 | + return (this.shouldStable) |
| 117 | + ? stable === true |
| 118 | + : true; |
| 119 | + }) |
| 120 | + .reduce((stack, { value }) => { |
| 121 | + return stack.concat(value); |
| 122 | + }, []); |
102 | 123 | ast.links = {}; |
103 | 124 | return marked.parser(ast); |
104 | 125 | } |
105 | 126 | }, |
106 | 127 | created: async function() { |
107 | | - const res = await axios.get(ConfigurationMdUrl); |
108 | | - const { |
109 | | - about, |
110 | | - configurationAbout, |
111 | | - configurationDescriptions |
112 | | - } = parseMarkdownAst(res.data); |
113 | | - this.aboutHtml = marked.parser(about); |
114 | | - this.configurationAboutHtml = marked.parser(configurationAbout); |
115 | | - this.configurationDescriptions = configurationDescriptions; |
| 128 | + const {data: tags} = await axios.get(RusfmtTagsUrl); |
| 129 | + const reMajorVersion = /v(\d+)/; |
| 130 | + const tagOptions = tags |
| 131 | + .map(tag => tag.name) |
| 132 | + .filter(tag => { |
| 133 | + const versionMatches = tag.match(reMajorVersion); |
| 134 | + if (!versionMatches || !versionMatches[1]) { |
| 135 | + return false; |
| 136 | + } |
| 137 | + const majorVersion = +versionMatches[1]; |
| 138 | + // There are some superfluous version tags (e.g. a v8.1 tag), so we do some |
| 139 | + // sanity checking of the tags here. |
| 140 | + return majorVersion >= MajorVersionBounds.min && |
| 141 | + majorVersion <= MajorVersionBounds.max; |
| 142 | + }); |
| 143 | + this.versionOptions = this.versionOptions.concat(tagOptions); |
116 | 144 | }, |
117 | 145 | mounted() { |
118 | 146 | if (UrlHash === '') return; |
|
0 commit comments