in site/src/shortcodes/includecode/transform.js [21:63]
function transform(githubCode) {
const parsedAST = parse(githubCode, {
sourceType: "module",
});
const isFirst = index => index === 0;
parsedAST.program.body.forEach((statement, index) => {
// The first set of lines usually have comments and we
// always want them removed
if(isFirst(index)) {
// Remove all comments before leading statement
if(statement.leadingComments) {
delete statement.leadingComments;
}
}
// Find any [START] or [END]
if(statement.leadingComments) {
statement.leadingComments = statement.leadingComments.filter(comment => {
return !comment.value.includes('[START') && !comment.value.includes('[END');
});
}
// Remove any trailing comments because they likely should be
// leading comments. Babel guesses where comments go and you can
// find a comment as both trailing and leading. This will likely
// cause problems in the future, but right now it works with the
// code samples we use.
if(statement.trailingComments) {
statement.trailingComments = [];
}
});
const { code } = generate({
type: "Program",
// passing a new copy of the body to avoid
// any reference problems
body: parsedAST.program.body.slice(),
});
return prettier.format(code, { parser: "babel" });
}