shd101wyy / vscode-markdown-preview-enhanced

One of the "BEST" markdown preview extensions for Visual Studio Code
https://shd101wyy.github.io/markdown-preview-enhanced
Other
1.48k stars 173 forks source link

[File-Import]: Indent not Correctly Handled #912

Open chezwicker opened 3 years ago

chezwicker commented 3 years ago

Introduction

I have a somewhat weird setup where I have to mix TEX into my markdown files in order to finally generate specifically formatted PDF.

Problem

When I use @import to include code snippets in a code block, the indentation is not preserved, which has two disadvantages:

Even weirder is that the indentation seems to be applied in the first @import, but not in the subsequent ones.

Example

js.plain

// single line comment
/* multi-line comment */

haskell.plain

– – single line comment
{- multi-line comment -}

First Attempt

main.md

\begin{codecompare}
\begin{code}{JavaScript}
@import "./js.plain" {code_block=false}
\end{code}
& \mbox{} &
\begin{code}{Haskell}
@import "./haskell.plain" {code_block=false}
\end{code}
\end{codecompare}

This results in the following output:

output.tex

\begin{codecompare}
\begin{code}{Pseudocode}
// single line comment
/* multi-line comment */
\end{code}
& \mbox{} &
\begin{code}{JavaScript}
– – single line comment
{- multi-line comment -}
\end{code}
\end{codecompare}

Second Attempt

main.md

\begin{codecompare}
\begin{code}{JavaScript}
@import "./js.plain" {code_block=false}
\end{code}
& \mbox{} &
\begin{code}{Haskell}
@import "./haskell.plain" {code_block=false}
\end{code}
\end{codecompare}

This results in the following output:

output.tex

\begin{codecompare}
\begin{code}{Pseudocode}
// single line comment
/* multi-line comment */
\end{code}
& \mbox{} &
\begin{code}{JavaScript}
– – single line comment
{- multi-line comment -}
\end{code}
\end{codecompare}

Request

What I would really like is to be able to write code like in "First Attempt" and get the following result:

output.tex

\begin{codecompare}
\begin{code}{Pseudocode}
// single line comment
/* multi-line comment */
\end{code}
& \mbox{} &
\begin{code}{JavaScript}
– – single line comment
{- multi-line comment -}
\end{code}
\end{codecompare}

In other word, indent in the original should be applied to every line of the imported file. In my example, four spaces would be prefixed to every line of the imported file.

In order to not break existing setups, I would suggest one of two options:

I believe the first option would have the least impact and could just be added to https://github.com/shd101wyy/mume/blob/master/src/transformer.ts#L939 using the (I assume) existing indenter.

Thanks

Thank you for your work and for considering this!

chezwicker commented 3 years ago

I just realized there is another issue: code_block=false gets evaluated after file extensions, so it seems it's not possible to use real file extensions if one wants to just import plain text. Could the condition be moved up? If not, maybe the second option would be better, allowing to always import plain text regardless of the file extension!