← Back to challenges
← Euler's Phi Function·Numbers That are Also Writers →

Format Blog Posts in Markdown

JavaScriptHardarraysdatesformattingobjects

Instructions

You are tasked with grabbing some blog posts from an API and formatting them using markdown, so that your coworker (who has no idea what markdown is) can publish them with ease on your company's website.

You already figured out how to get the posts and the associated users from the API, now you just need to write a function that takes that data and formats it in a markdown string. The rules are as follows:

  • The title should start with an uppercase character and be displayed as an H1.

  • Add 2 newlines after the title.

  • The metadata of the post has the following format: Written by <author> on <date>

    • The <author> part is a link where the text is the author's firstName and lastName (separated by a space), while the href is a mailto:<author email>.
    • The <date> part is the date of the post and the format is the following: <weekday>, <month> <date>, <year>
      • Weekday is in short form (Thu, Sun, …)
      • Month is in long form (April, December, …)
      • Date is in 2 digits form (01 - 31)
      • Year is always in extended form (2018)
  • Add 2 newlines after the metadata.

  • The separator is an horizontal line, in markdown it's written like this: ---.

  • Add 2 newlines after the separator.

  • The body of the post is already properly formatted and is the last thing you must add.

Post

PropertyTypeDescription
idnumberid of the post
userIdnumberid of the user that authored the post
timestampnumbertimestamp of the post (in milliseconds)
titlestringtitle of the post
bodystringbody (content) of the post

User

PropertyTypeDescription
idnumberid of the user
firstNamestringfirst name of the user
lastNamestringlast name of the user
emailstringemail of the user

Examples

Input:

formatBlogPost(
  {
    id: 1,
    userId: 10,
    timestamp: 1536581919628,
    title: 'varius ut blandit non interdum in ante',
    body:
      'Nullam sit amet turpis elementum ligula vehicula consequat. Morbi a ipsum. Integer a nibh.\n\nIn quis justo. Maecenas rhoncus aliquam lacus. Morbi quis tortor id nulla ultrices aliquet.',
  },
  users, // Array of objects, see structure above
);

Output (as a string):

# Varius ut blandit non interdum in ante

Written by [Sigismond Reavell](mailto:[email protected]) on Mon, September 10, 2018

---

Nullam sit amet turpis elementum ligula vehicula consequat. Morbi a ipsum. Integer a nibh.

In quis justo. Maecenas rhoncus aliquam lacus. Morbi quis tortor id nulla ultrices aliquet.

Notes

  • Feel like formatting the dates is tedious? It doesn't have to be! Some elements can be written in multiple styles, so be careful to use the correct one for this challenge.
javascript
Loading editor…
⌘ ↵ to run
Walks through the solution with reasoning and edge cases.