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

How to avoid escape for pre? #340

Open
jet10000 opened this issue Jul 7, 2020 · 1 comment
Open

How to avoid escape for pre? #340

jet10000 opened this issue Jul 7, 2020 · 1 comment

Comments

@jet10000
Copy link

jet10000 commented Jul 7, 2020

TurndownService.prototype.escape

        turndownService.addRule('strikethrough', {
            filter: ['pre'],
            replacement: function (content) {
                return '```\n' + content + '\n```'
            }
        });

I add a rule for pre, how to avoid escape for pre?

@martincizek
Copy link
Collaborator

If your intention is just to use just plain text content, which seems likely, then it is better to avoid the rendered content completely in your rule. So your example would look like

        turndownService.addRule('pre', {
            filter: 'pre',
            replacement: function (content, node) {
                // Note this is not bulletproof, see below.
                return '```\n' + node.textContent + '\n```'
            }
        });

To make it bulletproof, you need to deal with eventual fence string (```), which may occur within <pre>, and there are also a few considerations regarding leading and trailing newlines. All these things should now be covered in the standard fencedCodeBlock rule: https://github.com/domchristie/turndown/blob/master/src/commonmark-rules.js#L111

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

2 participants