From 35c8a954d00fa8ee70ede12565ee496f5d862820 Mon Sep 17 00:00:00 2001 From: Christian Morpurgo Date: Thu, 24 Apr 2025 14:18:29 +0200 Subject: [PATCH] add feature: setting to increase font scale --- MarkdownLivePreview.py | 16 +++++++++++++--- MarkdownLivePreview.sublime-settings | 5 ++++- markdown2html.py | 9 +++++++-- 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/MarkdownLivePreview.py b/MarkdownLivePreview.py index c23f276..5d41c62 100644 --- a/MarkdownLivePreview.py +++ b/MarkdownLivePreview.py @@ -20,6 +20,7 @@ from .markdown2html import markdown2html MARKDOWN_VIEW_INFOS = "markdown_view_infos" PREVIEW_VIEW_INFOS = "preview_view_infos" SETTING_DELAY_BETWEEN_UPDATES = "delay_between_updates" +SETTING_FONT_SCALE = "font_scale" resources = {} @@ -201,7 +202,12 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): # This check is needed since a this function is used as a callback for when images # are loaded from the internet (ie. it could finish loading *after* the user # closes the markdown_view) - if time.time() - self.last_update < DELAY / 1000: + # Reload settings each time to catch changes + settings = get_settings() + delay = settings.get(SETTING_DELAY_BETWEEN_UPDATES, 100) # Provide default + font_scale = settings.get(SETTING_FONT_SCALE, 1.0) # Provide default + + if time.time() - self.last_update < delay / 1000: return if markdown_view.buffer_id() == 0: @@ -213,15 +219,19 @@ class MarkdownLivePreviewListener(sublime_plugin.EventListener): markdown = markdown_view.substr(total_region) preview_view = markdown_view.window().active_view_in_group(1) - viewport_width = preview_view.viewport_extent()[0] + # Get viewport_width, default to a large value if view isn't ready + viewport_extent = preview_view.viewport_extent() + viewport_width = viewport_extent[0] if viewport_extent else 1024 - basepath = os.path.dirname(markdown_view.file_name()) + + basepath = os.path.dirname(markdown_view.file_name()) if markdown_view.file_name() else '.' # Handle unsaved files html = markdown2html( markdown, basepath, partial(self._update_preview, markdown_view), resources, viewport_width, + font_scale, ) self.phantom_sets[markdown_view.id()].update( diff --git a/MarkdownLivePreview.sublime-settings b/MarkdownLivePreview.sublime-settings index 9715073..8e77e84 100644 --- a/MarkdownLivePreview.sublime-settings +++ b/MarkdownLivePreview.sublime-settings @@ -1,4 +1,7 @@ { // minimum number of milliseconds to wait before updating the preview again - "delay_between_updates": 100 + "delay_between_updates": 100, + + // scale factor for the font size relative to markdown settings + "font_scale": 1.0 } \ No newline at end of file diff --git a/markdown2html.py b/markdown2html.py index 4e70cbd..2ef3cb8 100644 --- a/markdown2html.py +++ b/markdown2html.py @@ -25,7 +25,7 @@ markdowner = Markdown(extras=["fenced-code-blocks", "cuddled-lists"]) # does it stupidly throw them out? (we could implement something of our own) executor = concurrent.futures.ThreadPoolExecutor(max_workers=5) -def markdown2html(markdown, basepath, re_render, resources, viewport_width): +def markdown2html(markdown, basepath, re_render, resources, viewport_width, font_scale=1.0): """ converts the markdown to html, loads the images and puts in base64 for sublime to understand them correctly. That means that we are responsible for loading the images from the internet. Hence, we take in re_render, which is just a function we @@ -84,7 +84,11 @@ def markdown2html(markdown, basepath, re_render, resources, viewport_width): # FIXME: highlight the code using Sublime's syntax # FIXME: report that ST doesn't support
but does work with
... WTF? - return "\n\n{}".format(resources["stylesheet"], soup).replace( + # Add font scaling CSS rule + font_scale_css = "body {{ font-size: {}em; }}\n".format(font_scale) + stylesheet = font_scale_css + resources["stylesheet"] + + return "\n\n{}".format(stylesheet, soup).replace( "
", "
" ) @@ -218,4 +222,5 @@ def independent_markdown2html(markdown): "stylesheet": "", }, 960, + 1.0, # Add default font_scale for independent call )