Skip to content

Commit

Permalink
[#4429] Fix Error Retrieving Old Config Options
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jdollar committed Oct 3, 2020
1 parent 22c4437 commit 1926a21
Showing 1 changed file with 139 additions and 77 deletions.
216 changes: 139 additions & 77 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,103 @@
return `v${version}`;
};
const versionNumber = null !== versionParam ? parseVersionParam(versionParam) : 'master';
const determineConfigurationOptions = (version) => {
const repoBaseUrl = 'https://raw.githubusercontent.com/rust-lang/rustfmt';

const groupByDepth = (depth) => (stack, next) => {
if (next.depth === depth) {
stack.push([]);
}

const lastIndex = stack.length - 1;
stack[lastIndex].push(next);
return stack;
};

const groupByDepthOne = groupByDepth(1);
const groupByDepthTwo = groupByDepth(2);

switch (version) {
case 'v0.8.1':
case 'v0.7':
return {
createHeadAndValue: (ast) => {
const depthTwos = ast.reduce(groupByDepthTwo, [[]])

// Old READMEs have a link to a source file that won't link correctly on the doc page.
// Redirecting to the raw version explicitly
const fixSrcLinks = (element) => {
if (element.tokens) {
return {
...element,
tokens: element.tokens.map((token) => {
if (token.type === 'link') {
return {
...token,
href: token.href.replace(/src\//, `${repoBaseUrl}/${version}/src/`),
};
}

return token;
})
};
}

return element;
};

const about = depthTwos
.filter(curr => {
return curr[0].text.includes('Configuring Rustfmt')
})
.map((elems) => elems.map(fixSrcLinks))
.map((elems) => {
return ({
head: elems[0].text,
value: elems,
stable: false,
text: elems
.map(val => val.text)
.filter(val => val != null)
.join(' ')
})
});

return [
about,
[{ value: {} }], // No subsections for configuration on old READMEs
];
},
configUrl: `${repoBaseUrl}/${version}/README.md`,
};
default:
return {
createHeadAndValue: (ast) => {
const depthOnes = ast.reduce(groupByDepthOne, []);
const depthTwos = depthOnes.map((elem) => elem.reduce(groupByDepthTwo, [[]]));

return depthTwos.map((elem) => {
return elem.map((val) => {
return {
head: val[0].text,
value: val,
stable: val.some((elem) => {
return elem.type === "list" &&
!!elem.raw &&
elem.raw.includes("**Stable**: Yes");
}),
text: val
.map(item => item.text)
.filter(text => text != null)
.join(' ')
}
});
})
},
configUrl: `${repoBaseUrl}/${version}/Configurations.md`
};
}
}
new Vue({
el: '#app',
data: {
Expand All @@ -114,11 +211,10 @@
asyncComputed: {
async outputHtml() {
if (this.version !== this.oldVersion) {
const ConfigurationMdUrl =
`https://raw.githubusercontent.com/rust-lang/rustfmt/${this.version}/Configurations.md`;
const configOptions = determineConfigurationOptions(this.version);
let res;
try {
res = await axios.get(ConfigurationMdUrl).catch(e => { throw e });
res = await axios.get(configOptions.configUrl).catch(e => { throw e });
} catch(e) {
this.handleReqFailure(e);
return;
Expand All @@ -127,28 +223,16 @@
about,
configurationAbout,
configurationDescriptions
} = parseMarkdownAst(res.data);
} = parseMarkdownAst(configOptions, res.data);

this.aboutHtml = marked.parser(about);
this.configurationAboutHtml = marked.parser(configurationAbout);
this.configurationAboutHtml = configurationAbout
? marked.parser(configurationAbout)
: '';
this.configurationDescriptions = configurationDescriptions;
this.oldVersion = this.version;
}

const ast = this.configurationDescriptions
.filter(({ head, text, stable }) => {
if (text.includes(this.searchCondition) === false &&
head.includes(this.searchCondition) === false) {
return false;
}
return (this.shouldStable)
? stable === true
: true;
})
.reduce((stack, { value }) => {
return stack.concat(value);
}, []);
ast.links = {};

queryParams.set('version', this.version);
queryParams.set('search', this.searchCondition);
const curUrl = window.location.pathname +
Expand All @@ -163,15 +247,38 @@
</h${level}>`;
};

return marked.parser(ast, {
highlight(code, lang) {
return hljs.highlight(lang ? lang : 'rust', code).value;
},
headerIds: true,
headerPrefix: '',
renderer,
});
}
let output = '';
if (this.configurationDescriptions && this.configurationDescriptions.length > 0) {
const ast = this.configurationDescriptions
.filter(({ head, text, stable }) => {
if (text &&
head &&
text.includes(this.searchCondition) === false &&
head.includes(this.searchCondition) === false) {
return false;
}
return (this.shouldStable)
? stable === true
: true;
})
.reduce((stack, { value }) => {
return stack.concat(value);
}, []);

ast.links = {};

output = await marked.parser(ast, {
highlight(code, lang) {
return hljs.highlight(lang ? lang : 'rust', code).value;
},
headerIds: true,
headerPrefix: '',
renderer,
});
}

return output;
},
},
created: async function() {
let tags;
Expand Down Expand Up @@ -222,56 +329,11 @@
}
}
});
const extractDepthOnes = (ast) => {
return ast.reduce((stack, next) => {
if (next.depth === 1) {
stack.push([]);
}
const lastIndex = stack.length - 1;
stack[lastIndex].push(next);
return stack;
}, []);
}
const extractDepthTwos = (ast) => {
return ast.map((elem) => {
return elem.reduce((stack, next) => {
if (next.depth === 2) {
stack.push([]);
}
const lastIndex = stack.length - 1;
stack[lastIndex].push(next);
return stack;
},
[[]]);
});
}
const createHeadAndValue = (ast) => {
return ast.map((elem) => {
return elem.map((val) => {
return {
head: val[0].text,
value: val,
stable: val.some((elem) => {
return elem.type === "list" &&
!!elem.raw &&
elem.raw.includes("**Stable**: Yes");
}),
text: val.reduce((result, next) => {
return next.text != null
? `${result} ${next.text}`
: result;
}, '')
}
});
})
}
const parseMarkdownAst = (rawMarkdown) => {
const parseMarkdownAst = ({ createHeadAndValue }, rawMarkdown) => {
const ast = marked.lexer(rawMarkdown);
const depthOnes = extractDepthOnes(ast);
const depthTwos = extractDepthTwos(depthOnes);
const [
abouts, configurations
] = createHeadAndValue(depthTwos);
] = createHeadAndValue(ast);
const about = abouts[0].value;
about.links = {};
const [
Expand All @@ -281,7 +343,7 @@

return {
about,
configurationAbout: configurationAbout.value,
configurationAbout: configurationAbout,
configurationDescriptions
};
}
Expand Down

0 comments on commit 1926a21

Please sign in to comment.