Skip to content

Commit 7ff06ea

Browse files
authored
Fix code emphasis (again) (#44)
The original fix handled a special case where emphasis can be preserved, but in cases where only part of the code block is emphasized there isn't a way to preserve it. In those cases just drop it entirely. Fixes #42
1 parent db1a449 commit 7ff06ea

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

src/content_scripts/to-markdown.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,16 @@ function generateMarkdown(doc) {
2121
// and generates files that render well on GitHub.
2222
turndownService.use(gfm);
2323

24-
// Special handling for emphasized code blocks (e.g.
25-
// `<code><em>1234<em></code>`). The default results in the underscores being
26-
// rendered (e.g. `_1234_`), but by inverting the tags it will emphasize the
27-
// code in the block (e.g. _`1234`_).
24+
// Special handling for emphasized code blocks.
25+
//
26+
// ```html
27+
// <code><em>1234<em></code>
28+
// ```
29+
//
30+
// The default results in the underscores being rendered within the code
31+
// block. Since the entire code block should be emphasized a simple solution
32+
// is to just move the emphasis outside the block which appears to create the
33+
// correct result (on GitHub at least).
2834
turndownService.addRule("emphasizedCode", {
2935
filter: (node) =>
3036
node.nodeName == "CODE" &&
@@ -36,6 +42,26 @@ function generateMarkdown(doc) {
3642
`${options.emDelimiter}\`${node.innerText}\`${options.emDelimiter}`,
3743
});
3844

45+
// Special handling for emphasized text within code blocks.
46+
//
47+
// ```html
48+
// <code>1 + 1 = <em>2</em></code>
49+
// ```
50+
//
51+
// The default rendering results in the underscores being rendered, as there
52+
// isn't a proper way to render emphasis within code blocks without resorting
53+
// to HTML again. In this case the best solution is to drop the emphasis
54+
// completely, rendering the content without any markup.
55+
//
56+
// Since the rules are applied in order this should only consider instances
57+
// where the previous code did not make a modification.
58+
turndownService.addRule("emphasisWithinCode", {
59+
filter: (node) =>
60+
node.nodeName == "EM" &&
61+
node.parentNode.nodeName == "CODE",
62+
replacement: (content) => content,
63+
});
64+
3965
// Keep the spans which are used for alternate text in the puzzle description.
4066
turndownService.keep(["span"]);
4167

0 commit comments

Comments
 (0)