miaolz123 / vue-markdown

A Powerful and Highspeed Markdown Parser for Vue
https://miaolz123.github.io/vue-markdown/
MIT License
1.89k stars 257 forks source link

[Enhancement] Whitespace flooring option #78

Open FossPrime opened 5 years ago

FossPrime commented 5 years ago

I often find myself using this and having to ignore template indentation. It would be great to have a floor="true" option to automatically filter the input, count minimum indentation, and then crop all indentation to set that as the floor. That way, markdown actually works.

Example:

<template>
  <div>
  ... random html
  <vue-markdown floor="true">
    # Hello 
    - Bulletman
  </vue-markdown>
  </div>
</template>

The input would be filtered to:

Hello

instead of

__# Hello
__- Bulletman

Proposed code: I'm sure there is a faster non regex way:

floorS (s) {
  const n = s ? s.match(/\n */g).sort((a,b) => a.length > b.length).pop().length : 0
  return s ? s.split('/s').map(a => a.substring(n-1)).join(\n) : s
}

Not sure if we should support tabs... /\n[ |\t]*/g should do it