|
98 | 98 | return `v${version}`;
|
99 | 99 | };
|
100 | 100 | 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 | + } |
101 | 198 | new Vue({
|
102 | 199 | el: '#app',
|
103 | 200 | data: {
|
|
114 | 211 | asyncComputed: {
|
115 | 212 | async outputHtml() {
|
116 | 213 | 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); |
119 | 215 | let res;
|
120 | 216 | try {
|
121 |
| - res = await axios.get(ConfigurationMdUrl).catch(e => { throw e }); |
| 217 | + res = await axios.get(configOptions.configUrl).catch(e => { throw e }); |
122 | 218 | } catch(e) {
|
123 | 219 | this.handleReqFailure(e);
|
124 | 220 | return;
|
|
127 | 223 | about,
|
128 | 224 | configurationAbout,
|
129 | 225 | configurationDescriptions
|
130 |
| - } = parseMarkdownAst(res.data); |
| 226 | + } = parseMarkdownAst(configOptions, res.data); |
| 227 | + |
131 | 228 | this.aboutHtml = marked.parser(about);
|
132 |
| - this.configurationAboutHtml = marked.parser(configurationAbout); |
| 229 | + this.configurationAboutHtml = configurationAbout |
| 230 | + ? marked.parser(configurationAbout) |
| 231 | + : ''; |
133 | 232 | this.configurationDescriptions = configurationDescriptions;
|
134 | 233 | this.oldVersion = this.version;
|
135 | 234 | }
|
136 | 235 |
|
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 |
| - |
152 | 236 | queryParams.set('version', this.version);
|
153 | 237 | queryParams.set('search', this.searchCondition);
|
154 | 238 | const curUrl = window.location.pathname +
|
|
163 | 247 | </h${level}>`;
|
164 | 248 | };
|
165 | 249 |
|
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 | + }, |
175 | 282 | },
|
176 | 283 | created: async function() {
|
177 | 284 | let tags;
|
|
222 | 329 | }
|
223 | 330 | }
|
224 | 331 | });
|
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) => { |
269 | 333 | const ast = marked.lexer(rawMarkdown);
|
270 |
| - const depthOnes = extractDepthOnes(ast); |
271 |
| - const depthTwos = extractDepthTwos(depthOnes); |
272 | 334 | const [
|
273 | 335 | abouts, configurations
|
274 |
| - ] = createHeadAndValue(depthTwos); |
| 336 | + ] = createHeadAndValue(ast); |
275 | 337 | const about = abouts[0].value;
|
276 | 338 | about.links = {};
|
277 | 339 | const [
|
|
281 | 343 |
|
282 | 344 | return {
|
283 | 345 | about,
|
284 |
| - configurationAbout: configurationAbout.value, |
| 346 | + configurationAbout: configurationAbout, |
285 | 347 | configurationDescriptions
|
286 | 348 | };
|
287 | 349 | }
|
|
0 commit comments