regexpcreoniguruma

Convert GFM highlighted code block to Stack Overflow highlighted code block


1. Question

I can't convert GFM highlighted code block to Stack Overflow highlighted code block.


2. Example

For example, I need convert:

Do not change this line

```markdown
Sasha great!
    Sasha nice!
She is beautiful, surprise!
```

Do not change this line

to:

Do not change this line

<!-- language: lang-markdown -->

    Sasha great!
        Sasha nice!
    She is beautiful, surprise!

Do not change this line

3. Problem

That get highlighted code block, I need to add tab in beginning of each line inside code block. I don't understand, how I can do it.


4. Not helped

my example regex:

Demonstration on Regex101.

I get result:

Do not change this line

<!-- language: lang-markdown -->

    Sasha great!
    Sasha nice!
She is beautiful, surprise!


Do not change this line

Tabulation symbol added in beginning only for first line inside code block. What can I do, that add tab symbol in beginning of each line inside code block?


Solution

  • Since you are using Sublime Text find / replace functionality and there is no programming language involved it would take you about two steps to achieve what you desire.

    For first step try to search for:

    (?m)(?:^```\h*\S+\s+\K|\G(?!\A))^(?!```)(.*\R+)(?:```)?
    

    and replace with:

    \t\1
    

    Live demo

    Second find / replace process would be for adding HTML comment so search for:

    (?m)^```\h*(\S+)
    

    and replace it with:

    <!-- language: lang-\1 -->\n
    

    Live demo