slidevjs / slidev

Presentation Slides for Developers
https://sli.dev
MIT License
32.8k stars 1.33k forks source link

Interaction between preprocessor/preparser and slide editor #1579

Open gureckis opened 4 months ago

gureckis commented 4 months ago

Describe the bug This one is a little tricky and I apologize if misunderstanding how things should work. The preparser extensions (documented here) imply that your .md file is read in and then processed prior to be rendered. However, when using slide edit mode, the preprocessed file is written back out to disk. For example, in the example 1 where you replace your compact syntax notation, the idea is to make your markdown shorter. However, if you were to run this, then open the code in the slide editor would immediately overwrite your compact .md file with the expanded versions of these macros.

To Reproduce Steps to reproduce the behavior:

  1. add a setup/preparser.ts file with something that adds new lines to the .md during preprocessing.
import { definePreparserSetup } from '@slidev/types'

export default definePreparserSetup(({ filepath, headmatter, mode }) => {
  return [
    {
      transformRawLines(lines) {
        // if front matter exists then insert at the top
        // assumes you correctly ended the front matter with '---'
        if (lines[0] === '---') {
          lines.splice(1, 0, `filename: ${filepath}`)
        } else {
          // otherwise add frontmatter to first slide containing the file path
          lines.unshift(`---`)
          lines.unshift(`filename: ${filepath}`)
          lines.unshift(`---`)
        }
      },
    },
  ]
})
  1. open the slide in the slide editor
  2. observe that the .md file now has filename: XXX in the front matter defined twice (also there start being errors from the front matter preprocessor because in this case the change introduced two variables with the same name).

My expectation is that opening the slide editor while using the preparser shouldn't interact. Alternatively, this issue could be expected behavior in which case I'd mention that people should view the preprocessor as being applied multiple times to your file in the event you open the slide editor (so the code needs extra logic to handle that). Alternatively, the slidev should somehow write out to disk only the changes to the file from the slide editor but none of the changes rendered by the preparser.

Desktop (please complete the following information):

twitwi commented 1 month ago

The preparser is for very specific uses cases and indeed breaks editor integration (we might want to highlight it more clearly in the documentation).

Having a full round trip edition is way too much work (and technically not always possible) compared to the gain.

I guess an improvement (still not so easy) would be to disallow the edition of slides in files that have been preparsed.