Skip to content

Tabs within code block are converted to 4 spaces? #644

Closed

Description

@reggi

You can see I have to code blocks one with a two space indentation and one with a tab. The tab is oddly converted to 4 spaces.

var marked = require('marked')
var Promise = require('bluebird')
var markedAsync = Promise.promisify(marked)
var markdown = [
  '```js',
  'if (true) {',
  '   console.log(\'word spaces\')',
  '}',
  '```',
  '```js',
  'if (true) {',
  ' console.log(\'word tabs\')',
  '}',
  '```'
].join('\n')
console.log(JSON.stringify(markdown))
markedAsync(markdown)
.then(function (html) {
  console.log(JSON.stringify(html))
})
"```js\nif (true) {\n  console.log('word spaces')\n}\n```\n```js\nif (true) {\n\tconsole.log('word tabs')\n}\n```"
"<pre><code class=\"lang-js\">if (true) {\n  console.log(&#39;word spaces&#39;)\n}\n</code></pre>\n<pre><code class=\"lang-js\">if (true) {\n    console.log(&#39;word tabs&#39;)\n}\n</code></pre>\n"

Activity

  1. mbovel commented on Mar 18, 2016

    @mbovel

    Same problem here. I think this is definitely a bug: code blocks should not be modified in any way. That's the behaviour promoted by CommonMark.

    Discount and Pandoc, however, also seem to convert tabs to spaces.

  2. joshbruce commented on Dec 25, 2017

    @joshbruce
    Member
  3. Feder1co5oave commented on Jan 10, 2018

    @Feder1co5oave
    Contributor

    You're right, commonmark says that. However, marked heavily relies on the fact that everything is indented with spaces. Code blocks, list, blockquotes...
    I don't think this can be changed without a massive review of the source code.

  4. vixalien commented on Dec 18, 2021

    @vixalien

    If it is not possible to let tabs remain the same, atleast allow the user the select the number of spaces they want.

  5. UziTech commented on Dec 18, 2021

    @UziTech
    Member

    @vixalien you can convert them before you send the markdown to marked.

    // tab is 2 spaces
    marked.parse(markdown.replace(/\t/g, '  '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions