Back to Freecodecamp

Challenge 164: Markdown Inline Code Parser

curriculum/challenges/english/blocks/daily-coding-challenges-python/694596b0585c11170ac7c7fc.md

latest1.9 KB
Original Source

--description--

Given a string of Markdown that includes one or more inline code blocks, return the equivalent HTML string.

Inline code blocks in Markdown use a single backtick (`) at the start and end of the code block text.

Return the given string with all code blocks converted to HTML code tags.

For example, given the string "Use `let` to declare the variable.", return "Use <code>let</code> to declare the variable.".

Note: The console may not display HTML tags in strings when logging messages. Check the browser console to see logs with tags included.

--hints--

parse_inline_code("Use `let` to declare the variable.") should return "Use <code>let</code> to declare the variable.".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_inline_code("Use \`let\` to declare the variable."), "Use <code>let</code> to declare the variable.")`)
}})

parse_inline_code("Use `let` or `const` to declare a variable.") should return "Use <code>let</code> or <code>const</code> to declare a variable.".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_inline_code("Use \`let\` or \`const\` to declare a variable."), "Use <code>let</code> or <code>const</code> to declare a variable.")`)
}})

parse_inline_code("Run `npm install` then `npm start`.") should return "Run <code>npm install</code> then <code>npm start</code>.".

js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(parse_inline_code("Run \`npm install\` then \`npm start\`."), "Run <code>npm install</code> then <code>npm start</code>.")`)
}})

--seed--

--seed-contents--

py
def parse_inline_code(markdown):

    return markdown

--solutions--

py
import re
def parse_inline_code(markdown):
    return re.sub(r"`([^`]+)`", r"<code>\1</code>", markdown)