Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Very simple preformatted code block does not get converted properly #435

Open
dddoyle1 opened this issue May 3, 2023 · 3 comments
Open

Comments

@dddoyle1
Copy link

dddoyle1 commented May 3, 2023

I have a very simple element:

<pre>
<code class="">
fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
  print(fruit)
</code>
</pre>

that I would like converted to a fenced code block via TurndownService::turndown

I initialize the TurndownService with

var turndownService = new TurndowSnService({codeBlockStyle: "fenced"});

However, turndown yields the following string when I pass my element:

"fruits = [\"apple\", \"banana\", \"cherry\"]\n\nfor fruit in fruits:\n  print(fruit)"

I've verified by hand that the fencedCodeBlock.filter returns true when passed my element and that fencedCodeBlock.replacement returns the expected string sandwiched by the fence, but this behavior is not reproduced as expected when using TurndownService::turndown

@domchristie
Copy link
Collaborator

This looks like it's working on the live demo: https://mixmark-io.github.io/turndown/
The output is:

```

fruits = ["apple", "banana", "cherry"]

for fruit in fruits:
  print(fruit)
```

@dddoyle1
Copy link
Author

dddoyle1 commented May 3, 2023

Looks like this happens only when I pass the HTML element directly, whereas if I instead do

turndownService.turndown(element.outerHTML)

the element is converted as I expected. Is this intended behavior?

FWIW, I see that when I pass the element directly, the thing that makes it to fencedCodeBlock.filter is a node with "<pre>...</pre>" stripped away, ie a code node, which fails the filter criteria

@XinBaoCode
Copy link

The codeBlockStyle option will do this for you. For example:

var turndownService = new TurndownService({ codeBlockStyle: 'fenced' })
turndownService.turndown('<pre><code class="language-js">console.log("hello world")</code></pre>')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants