Convert HTML Code Elements to Markdown Code Syntax
Convert HTML <code>, <pre>, and <pre><code> elements to Markdown inline code and fenced code blocks. Includes language detection, entity decoding, and syntax highlighting hints.
Detailed Explanation
HTML Code Elements to Markdown
HTML uses <code> for inline code and <pre><code> for code blocks. Markdown uses backticks for inline code and triple backticks (or indentation) for code blocks.
Inline Code
<p>Use the <code>Array.map()</code> method to transform elements.</p>
Converts to:
Use the `Array.map()` method to transform elements.
The <code> tag is replaced with single backtick wrappers.
Code Blocks
<pre><code>function hello() {
console.log("Hello, world!");
}
</code></pre>
Converts to:
```
function hello() {
console.log("Hello, world!");
}
```
The <pre><code> combination is recognized as a code block and wrapped in triple backticks.
Language Detection
Many HTML code blocks include a language class for syntax highlighting:
<pre><code class="language-javascript">const x = 42;
</code></pre>
A good converter extracts the language hint:
```javascript
const x = 42;
```
Common class patterns include language-*, lang-*, and highlight-*.
HTML Entity Decoding
Code inside HTML often contains encoded entities:
<pre><code><div class="container">
&copy; 2024
</div>
</code></pre>
During conversion, entities must be decoded back to their literal characters:
```
<div class="container">
© 2024
</div>
```
< becomes <, > becomes >, & becomes &, and " becomes ".
Standalone pre Tags
A bare <pre> without <code> should also be converted to a code block, as it indicates preformatted text.
Use Case
Code block conversion is crucial when migrating technical documentation, API references, tutorials, and developer blog posts. Correct language detection preserves syntax highlighting, and proper entity decoding ensures code snippets remain functional.
Try It — HTML to Markdown
Related Topics
Decode HTML Entities During Markdown Conversion
Real-World HTML
Convert HTML Bold and Italic to Markdown Emphasis
Text Formatting
Convert HTML Links and Images to Markdown Syntax
Media & Links
Convert Scraped Web HTML to Structured Markdown
Real-World HTML
Convert Deeply Nested HTML to Clean Markdown
Real-World HTML