diff --git a/.sublime/MarkdownLivePreview.sublime-settings b/.sublime/MarkdownLivePreview.sublime-settings index 3b57d3b..2166833 100644 --- a/.sublime/MarkdownLivePreview.sublime-settings +++ b/.sublime/MarkdownLivePreview.sublime-settings @@ -8,5 +8,9 @@ // When the preview is opened, the markdown file is closed in the origin window and reopend in // the preview window. If this option is set to 'true', then the markdown file will NOT be // closed in the origin window - "keep_open_when_opening_preview": false + "keep_open_when_opening_preview": false, + + // Choose what to do with YAML/TOML (---/+++ respectively) headers + // Valid values: "wrap_in_pre", "remove". + "header_action": "wrap_in_pre" } diff --git a/MLPApi.py b/MLPApi.py index 28dc370..a893239 100644 --- a/MLPApi.py +++ b/MLPApi.py @@ -48,8 +48,12 @@ def get_style(): return content def markdown2html(md, basepath): + # removes/format the header. + md = manage_header(md, get_settings().get('header_action')) + html = '\n'.format(get_style()) + # the option no-code-highlighting does not exists in the official version of markdown2 for now # I personaly edited the file (markdown2.py:1743) html += md2.markdown(md, extras=['fenced-code-blocks', 'no-code-highlighting', 'tables']) diff --git a/functions.py b/functions.py index abe6f11..8ef73ab 100644 --- a/functions.py +++ b/functions.py @@ -11,12 +11,25 @@ def plugin_loaded(): loading = sublime.load_resource('Packages/MarkdownLivePreview/loading.txt') error404 = sublime.load_resource('Packages/MarkdownLivePreview/404.txt') +MATCH_YAML_HEADER = re.compile(r'^([\-\+])\1{2}\n(?P.+)\n\1{3}\n', re.DOTALL) + def strip_html_comments(html): soup = BeautifulSoup(html, 'html.parser') for element in soup.find_all(text=lambda text: isinstance(text, html_comment)): element.extract() return str(soup) +def manage_header(md, action): + matchobj = MATCH_YAML_HEADER.match(md) + if not matchobj: + return md + if action == 'remove': + return md[len(matchobj.group(0)):] + elif action == 'wrap_in_pre': + return '
' + matchobj.group('content') + '
' \ + + md[len(matchobj.group(0)):] + + raise ValueError('Got an unknown action: "{}"'.format(action)) def get_preview_name(md_view): file_name = md_view.file_name() diff --git a/sample.md b/sample.md index dca6978..8564d3a 100644 --- a/sample.md +++ b/sample.md @@ -1,7 +1,9 @@ --- title: Demo -description: Preview your markdown! +description: Preview your markdown right in Sublime Text! +hope: You'll enjoy using it! --- + # Hello world @@ -33,7 +35,7 @@ if you is moods.curious: | 45 | John | | `` | `><` | -[Sublime Text Logo](https://upload.wikimedia.org/wikipedia/en/4/4c/Sublime_Text_Logo.png) +![Sublime Text Logo](https://upload.wikimedia.org/wikipedia/en/4/4c/Sublime_Text_Logo.png) Some plugin I just *need*: