Skip to content

Instantly share code, notes, and snippets.

@alexe-dev
Created November 6, 2023 15:14
Show Gist options
  • Save alexe-dev/95a6f71057d711a0b1f6189bdd1e5497 to your computer and use it in GitHub Desktop.
Save alexe-dev/95a6f71057d711a0b1f6189bdd1e5497 to your computer and use it in GitHub Desktop.
Solution for 1st part metabase
/**
problem spec:
https://gist.github.com/ranquild/7d3b92d74322d430ea37d97a13868676
*/
import { sampleInput, sampleOutput } from "./samples";
const formatMD = (input) => {
const { content } = input;
return content.reduce((formattedMD, contentPart) => {
if (contentPart.tag === "heading") {
return `${formattedMD}# ${contentPart.content[0]}\n\n`;
}
if (contentPart.tag === "paragraph") {
const text = contentPart.content;
const formattedText = text.reduce((current, textPart) => {
if (typeof textPart === "string") {
return current + textPart;
}
if (typeof textPart === "object") {
if (textPart.tag === "bold") {
return `${current}**${textPart.content[0]}**`;
}
if (textPart.tag === "italic") {
return `${current}*${textPart.content[0]}*`;
}
}
return current;
}, "");
return formattedMD + formattedText + "\n\n";
}
return formattedMD;
}, "");
};
// Hi, I spent a few minutes after the meeting to fix the console ;)
console.log(formatMD(sampleInput));
console.log(sampleOutput === formatMD(sampleInput));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment