Skip to content

Commit 1926a21

Browse files
committed
[#4429] Fix Error Retrieving Old Config Options
Add code to the doc/index.html file to handle the specific cases for version 0.8.1 and 0.7 of rustfmt where they don't have a explicit configuration.md file. Updated to pull the data that is within the "Configuring Rustfmt" section of the original README
1 parent 22c4437 commit 1926a21

File tree

1 file changed

+139
-77
lines changed

1 file changed

+139
-77
lines changed

docs/index.html

Lines changed: 139 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,103 @@
9898
return `v${version}`;
9999
};
100100
const versionNumber = null !== versionParam ? parseVersionParam(versionParam) : 'master';
101+
const determineConfigurationOptions = (version) => {
102+
const repoBaseUrl = 'https://raw.githubusercontent.com/rust-lang/rustfmt';
103+
104+
const groupByDepth = (depth) => (stack, next) => {
105+
if (next.depth === depth) {
106+
stack.push([]);
107+
}
108+
109+
const lastIndex = stack.length - 1;
110+
stack[lastIndex].push(next);
111+
return stack;
112+
};
113+
114+
const groupByDepthOne = groupByDepth(1);
115+
const groupByDepthTwo = groupByDepth(2);
116+
117+
switch (version) {
118+
case 'v0.8.1':
119+
case 'v0.7':
120+
return {
121+
createHeadAndValue: (ast) => {
122+
const depthTwos = ast.reduce(groupByDepthTwo, [[]])
123+
124+
// Old READMEs have a link to a source file that won't link correctly on the doc page.
125+
// Redirecting to the raw version explicitly
126+
const fixSrcLinks = (element) => {
127+
if (element.tokens) {
128+
return {
129+
...element,
130+
tokens: element.tokens.map((token) => {
131+
if (token.type === 'link') {
132+
return {
133+
...token,
134+
href: token.href.replace(/src\//, `${repoBaseUrl}/${version}/src/`),
135+
};
136+
}
137+
138+
return token;
139+
})
140+
};
141+
}
142+
143+
return element;
144+
};
145+
146+
const about = depthTwos
147+
.filter(curr => {
148+
return curr[0].text.includes('Configuring Rustfmt')
149+
})
150+
.map((elems) => elems.map(fixSrcLinks))
151+
.map((elems) => {
152+
return ({
153+
head: elems[0].text,
154+
value: elems,
155+
stable: false,
156+
text: elems
157+
.map(val => val.text)
158+
.filter(val => val != null)
159+
.join(' ')
160+
})
161+
});
162+
163+
return [
164+
about,
165+
[{ value: {} }], // No subsections for configuration on old READMEs
166+
];
167+
},
168+
configUrl: `${repoBaseUrl}/${version}/README.md`,
169+
};
170+
default:
171+
return {
172+
createHeadAndValue: (ast) => {
173+
const depthOnes = ast.reduce(groupByDepthOne, []);
174+
const depthTwos = depthOnes.map((elem) => elem.reduce(groupByDepthTwo, [[]]));
175+
176+
return depthTwos.map((elem) => {
177+
return elem.map((val) => {
178+
return {
179+
head: val[0].text,
180+
value: val,
181+
stable: val.some((elem) => {
182+
return elem.type === "list" &&
183+
!!elem.raw &&
184+
elem.raw.includes("**Stable**: Yes");
185+
}),
186+
text: val
187+
.map(item => item.text)
188+
.filter(text => text != null)
189+
.join(' ')
190+
}
191+
});
192+
})
193+
},
194+
configUrl: `${repoBaseUrl}/${version}/Configurations.md`
195+
};
196+
}
197+
}
101198
new Vue({
102199
el: '#app',
103200
data: {
@@ -114,11 +211,10 @@
114211
asyncComputed: {
115212
async outputHtml() {
116213
if (this.version !== this.oldVersion) {
117-
const ConfigurationMdUrl =
118-
`https://raw.githubusercontent.com/rust-lang/rustfmt/${this.version}/Configurations.md`;
214+
const configOptions = determineConfigurationOptions(this.version);
119215
let res;
120216
try {
121-
res = await axios.get(ConfigurationMdUrl).catch(e => { throw e });
217+
res = await axios.get(configOptions.configUrl).catch(e => { throw e });
122218
} catch(e) {
123219
this.handleReqFailure(e);
124220
return;
@@ -127,28 +223,16 @@
127223
about,
128224
configurationAbout,
129225
configurationDescriptions
130-
} = parseMarkdownAst(res.data);
226+
} = parseMarkdownAst(configOptions, res.data);
227+
131228
this.aboutHtml = marked.parser(about);
132-
this.configurationAboutHtml = marked.parser(configurationAbout);
229+
this.configurationAboutHtml = configurationAbout
230+
? marked.parser(configurationAbout)
231+
: '';
133232
this.configurationDescriptions = configurationDescriptions;
134233
this.oldVersion = this.version;
135234
}
136235

137-
const ast = this.configurationDescriptions
138-
.filter(({ head, text, stable }) => {
139-
if (text.includes(this.searchCondition) === false &&
140-
head.includes(this.searchCondition) === false) {
141-
return false;
142-
}
143-
return (this.shouldStable)
144-
? stable === true
145-
: true;
146-
})
147-
.reduce((stack, { value }) => {
148-
return stack.concat(value);
149-
}, []);
150-
ast.links = {};
151-
152236
queryParams.set('version', this.version);
153237
queryParams.set('search', this.searchCondition);
154238
const curUrl = window.location.pathname +
@@ -163,15 +247,38 @@
163247
</h${level}>`;
164248
};
165249

166-
return marked.parser(ast, {
167-
highlight(code, lang) {
168-
return hljs.highlight(lang ? lang : 'rust', code).value;
169-
},
170-
headerIds: true,
171-
headerPrefix: '',
172-
renderer,
173-
});
174-
}
250+
let output = '';
251+
if (this.configurationDescriptions && this.configurationDescriptions.length > 0) {
252+
const ast = this.configurationDescriptions
253+
.filter(({ head, text, stable }) => {
254+
if (text &&
255+
head &&
256+
text.includes(this.searchCondition) === false &&
257+
head.includes(this.searchCondition) === false) {
258+
return false;
259+
}
260+
return (this.shouldStable)
261+
? stable === true
262+
: true;
263+
})
264+
.reduce((stack, { value }) => {
265+
return stack.concat(value);
266+
}, []);
267+
268+
ast.links = {};
269+
270+
output = await marked.parser(ast, {
271+
highlight(code, lang) {
272+
return hljs.highlight(lang ? lang : 'rust', code).value;
273+
},
274+
headerIds: true,
275+
headerPrefix: '',
276+
renderer,
277+
});
278+
}
279+
280+
return output;
281+
},
175282
},
176283
created: async function() {
177284
let tags;
@@ -222,56 +329,11 @@
222329
}
223330
}
224331
});
225-
const extractDepthOnes = (ast) => {
226-
return ast.reduce((stack, next) => {
227-
if (next.depth === 1) {
228-
stack.push([]);
229-
}
230-
const lastIndex = stack.length - 1;
231-
stack[lastIndex].push(next);
232-
return stack;
233-
}, []);
234-
}
235-
const extractDepthTwos = (ast) => {
236-
return ast.map((elem) => {
237-
return elem.reduce((stack, next) => {
238-
if (next.depth === 2) {
239-
stack.push([]);
240-
}
241-
const lastIndex = stack.length - 1;
242-
stack[lastIndex].push(next);
243-
return stack;
244-
},
245-
[[]]);
246-
});
247-
}
248-
const createHeadAndValue = (ast) => {
249-
return ast.map((elem) => {
250-
return elem.map((val) => {
251-
return {
252-
head: val[0].text,
253-
value: val,
254-
stable: val.some((elem) => {
255-
return elem.type === "list" &&
256-
!!elem.raw &&
257-
elem.raw.includes("**Stable**: Yes");
258-
}),
259-
text: val.reduce((result, next) => {
260-
return next.text != null
261-
? `${result} ${next.text}`
262-
: result;
263-
}, '')
264-
}
265-
});
266-
})
267-
}
268-
const parseMarkdownAst = (rawMarkdown) => {
332+
const parseMarkdownAst = ({ createHeadAndValue }, rawMarkdown) => {
269333
const ast = marked.lexer(rawMarkdown);
270-
const depthOnes = extractDepthOnes(ast);
271-
const depthTwos = extractDepthTwos(depthOnes);
272334
const [
273335
abouts, configurations
274-
] = createHeadAndValue(depthTwos);
336+
] = createHeadAndValue(ast);
275337
const about = abouts[0].value;
276338
about.links = {};
277339
const [
@@ -281,7 +343,7 @@
281343

282344
return {
283345
about,
284-
configurationAbout: configurationAbout.value,
346+
configurationAbout: configurationAbout,
285347
configurationDescriptions
286348
};
287349
}

0 commit comments

Comments
 (0)