Skip to content

Commit 6959d03

Browse files
ayazhafizcalebcartwright
authored andcommitted
Show configs from different versions on github pages
See https://gushiermainecoon.htmlpasta.com/ for a demo of this change. Part of rust-lang#4178
1 parent 667a2da commit 6959d03

File tree

1 file changed

+65
-37
lines changed

1 file changed

+65
-37
lines changed

docs/index.html

Lines changed: 65 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/3.0.1/github-markdown.css" />
66
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
77
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
8+
<script src="https://unpkg.com/[email protected]"></script>
89
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.18.0/axios.min.js"></script>
910
<style>
1011
@media (max-width: 767px) {
@@ -59,60 +60,87 @@
5960
<label for="stable">stable: </label>
6061
<input type="checkbox" id="stable" v-model="shouldStable">
6162
</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>
6271
</div>
6372
<div v-html="aboutHtml"></div>
6473
<div v-html="configurationAboutHtml"></div>
6574
<div v-html="outputHtml"></div>
6675
</article>
6776
</div>
6877
<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';
7080
const UrlHash = window.location.hash.replace(/^#/, '');
7181
new Vue({
7282
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']
8392
},
84-
computed: {
85-
outputHtml() {
86-
const ast = this.configurationDescriptions
87-
.filter(({ head, text, stable }) => {
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+
}
88109

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-
}, []);
110+
const ast = this.configurationDescriptions
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+
}, []);
102123
ast.links = {};
103124
return marked.parser(ast);
104125
}
105126
},
106127
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);
116144
},
117145
mounted() {
118146
if (UrlHash === '') return;

0 commit comments

Comments
 (0)