Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eb48b1c79f | |||
| 8317fa738c | |||
| 3be12b0539 | |||
| c92d78fb20 |
@ -8,5 +8,9 @@
|
|||||||
// When the preview is opened, the markdown file is closed in the origin window and reopend in
|
// 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
|
// the preview window. If this option is set to 'true', then the markdown file will NOT be
|
||||||
// closed in the origin window
|
// 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"
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,8 +48,12 @@ def get_style():
|
|||||||
return content
|
return content
|
||||||
|
|
||||||
def markdown2html(md, basepath):
|
def markdown2html(md, basepath):
|
||||||
|
# removes/format the header.
|
||||||
|
md = manage_header(md, get_settings().get('header_action'))
|
||||||
|
|
||||||
html = '<style>\n{}\n</style>\n'.format(get_style())
|
html = '<style>\n{}\n</style>\n'.format(get_style())
|
||||||
|
|
||||||
|
|
||||||
# the option no-code-highlighting does not exists in the official version of markdown2 for now
|
# the option no-code-highlighting does not exists in the official version of markdown2 for now
|
||||||
# I personaly edited the file (markdown2.py:1743)
|
# I personaly edited the file (markdown2.py:1743)
|
||||||
html += md2.markdown(md, extras=['fenced-code-blocks', 'no-code-highlighting', 'tables'])
|
html += md2.markdown(md, extras=['fenced-code-blocks', 'no-code-highlighting', 'tables'])
|
||||||
@ -77,7 +81,9 @@ def markdown2html(md, basepath):
|
|||||||
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
|
html = replace_img_src_base64(html, basepath=os.path.dirname(basepath))
|
||||||
|
|
||||||
# BeautifulSoup uses the <br/> but the sublime phantoms do not support them...
|
# BeautifulSoup uses the <br/> but the sublime phantoms do not support them...
|
||||||
html = html.replace('<br/>', '<br />')
|
html = html.replace('<br/>', '<br />').replace('<hr/>', '<hr />')
|
||||||
|
|
||||||
|
sublime.set_clipboard(html) # print
|
||||||
|
|
||||||
return html
|
return html
|
||||||
|
|
||||||
|
|||||||
18
README.md
18
README.md
@ -35,17 +35,25 @@ Once you're done, close whichever file and it'll close the entire window.
|
|||||||
|
|
||||||
### Settings
|
### Settings
|
||||||
|
|
||||||
|
To edit MarkdownLivePreview's settings, you just need to look in the command palette
|
||||||
|
`Preferences: MarkdownLivePreview Settings`, or from the menus:
|
||||||
|
*Preferences → Package Settings → MarkdownLivePreview → Settings*
|
||||||
|
|
||||||
|
Do not edit the left file (by default, you cannot), but edit the right file. This last file will
|
||||||
|
override the default one (on the left), and will be saved in your `User` folder, which makes it easy
|
||||||
|
to back up.
|
||||||
|
|
||||||
- `markdown_live_preview_on_open`: if set to `true`, as soon as you open a markdown file, the
|
- `markdown_live_preview_on_open`: if set to `true`, as soon as you open a markdown file, the
|
||||||
preview window will popup (thanks to[@ooing](https://github.com/ooing) for its
|
preview window will popup (thanks to[@ooing](https://github.com/ooing) for its
|
||||||
[suggestion](https://github.com/math2001/MarkdownLivePreview/issues/7#issue-199464852)). Default to
|
[suggestion](https://github.com/math2001/MarkdownLivePreview/issues/7#issue-199464852)). Default to
|
||||||
`false`
|
`false`
|
||||||
- `load_from_internet_when_starts`: every images that starts with any of the string specified in
|
- `load_from_internet_when_starts`: every images that starts with any of the string specified in
|
||||||
this list will be loaded from internet. Default to `["http://", "https://"]`
|
this list will be loaded from internet. Default to `["http://", "https://"]`
|
||||||
|
- `header_action`: If you're writing a blog with some markdown and a static website generator, you
|
||||||
Note: To edit your settings, search up in the command palette
|
probably have a YAML header. By default, this header will be displayed in a `pre` block. If you want
|
||||||
`Preferences: MarkdownLivePreview Settings`, or by using the menu:
|
to hide it, then just set the value to `remove`
|
||||||
*Preferences → Packages Settings → MarkdownLivePreview → Settings*;. It's not your global settings,
|
- `keep_open_when_opening_preview`: Each time the preview window is opened, the original markdown
|
||||||
but only the `MarkdownLivePreview`'s one
|
view is closed. If you want to keep it opened, just set this setting to `true`
|
||||||
|
|
||||||
### Syntax Specific Settings
|
### Syntax Specific Settings
|
||||||
|
|
||||||
|
|||||||
17
functions.py
17
functions.py
@ -11,12 +11,25 @@ def plugin_loaded():
|
|||||||
loading = sublime.load_resource('Packages/MarkdownLivePreview/loading.txt')
|
loading = sublime.load_resource('Packages/MarkdownLivePreview/loading.txt')
|
||||||
error404 = sublime.load_resource('Packages/MarkdownLivePreview/404.txt')
|
error404 = sublime.load_resource('Packages/MarkdownLivePreview/404.txt')
|
||||||
|
|
||||||
|
MATCH_YAML_HEADER = re.compile(r'^([\-\+])\1{2}\n(?P<content>.+)\n\1{3}\n', re.DOTALL)
|
||||||
|
|
||||||
def strip_html_comments(html):
|
def strip_html_comments(html):
|
||||||
soup = BeautifulSoup(html, 'html.parser')
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
for element in soup.find_all(text=lambda text: isinstance(text, html_comment)):
|
for element in soup.find_all(text=lambda text: isinstance(text, html_comment)):
|
||||||
element.extract()
|
element.extract()
|
||||||
return str(soup)
|
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 '<pre><code>' + matchobj.group('content') + '</code></pre>' \
|
||||||
|
+ md[len(matchobj.group(0)):]
|
||||||
|
|
||||||
|
raise ValueError('Got an unknown action: "{}"'.format(action))
|
||||||
|
|
||||||
def get_preview_name(md_view):
|
def get_preview_name(md_view):
|
||||||
file_name = md_view.file_name()
|
file_name = md_view.file_name()
|
||||||
@ -26,7 +39,7 @@ def get_preview_name(md_view):
|
|||||||
return name + ' - Preview'
|
return name + ' - Preview'
|
||||||
|
|
||||||
def replace_img_src_base64(html, basepath):
|
def replace_img_src_base64(html, basepath):
|
||||||
soup = BeautifulSoup(html)
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
load_from_internet_starters = get_settings().get('load_from_internet_when_starts')
|
load_from_internet_starters = get_settings().get('load_from_internet_when_starts')
|
||||||
for img in soup.find_all('img'):
|
for img in soup.find_all('img'):
|
||||||
if img['src'].startswith('data:image/'):
|
if img['src'].startswith('data:image/'):
|
||||||
@ -92,7 +105,7 @@ def get_settings():
|
|||||||
def pre_with_br(html):
|
def pre_with_br(html):
|
||||||
"""Because the phantoms of sublime text does not support <pre> blocks
|
"""Because the phantoms of sublime text does not support <pre> blocks
|
||||||
this function replaces every \n with a <br> in a <pre>"""
|
this function replaces every \n with a <br> in a <pre>"""
|
||||||
soup = BeautifulSoup(html)
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
for pre in soup.find_all('pre'):
|
for pre in soup.find_all('pre'):
|
||||||
code = pre.find('code')
|
code = pre.find('code')
|
||||||
code.replaceWith(BeautifulSoup(''.join(str(node) for node in pre.contents) \
|
code.replaceWith(BeautifulSoup(''.join(str(node) for node in pre.contents) \
|
||||||
|
|||||||
@ -1,3 +1,9 @@
|
|||||||
|
---
|
||||||
|
title: Demo
|
||||||
|
description: Preview your markdown right in Sublime Text!
|
||||||
|
hope: You'll enjoy using it!
|
||||||
|
---
|
||||||
|
|
||||||
# Hello world
|
# Hello world
|
||||||
|
|
||||||
<!-- supports comments -->
|
<!-- supports comments -->
|
||||||
@ -20,6 +26,7 @@ if you is moods.curious:
|
|||||||
- you need
|
- you need
|
||||||
- todos
|
- todos
|
||||||
|
|
||||||
|
|
||||||
| ID | Name |
|
| ID | Name |
|
||||||
|-----------|-------|
|
|-----------|-------|
|
||||||
| 56 | Matt |
|
| 56 | Matt |
|
||||||
|
|||||||
Reference in New Issue
Block a user