Sublime Forum

Export part of the *.md file

#1

Hi,

I am not programmer, so hope to find some help here

I have a bunch of *.md files in ST. Their content is mainly

block quote text

and normal text

Is it possible to export (I have pandoc installed) just normal text?

Like this:

1

EDIT: I am on WIN 10 and ST 3

0 Likes

#2

You can write a simple plugin + keybinding to do that.

filter_content_by_scopes.py

import sublime
import sublime_plugin


class FilterContentByScopesCommand(sublime_plugin.TextCommand):
    def run(self, edit, skip_scopes=None):
        class_name = __class__.__name__
        v = self.view

        if not isinstance(skip_scopes, list):
            sublime.error_message(
                '[{}] "skip_scopes" must be a list.'.format(class_name)
            )
            return

        skip_scope = "(" + "), (".join(skip_scopes) + ")"

        # region, scope = token
        tokens = v.extract_tokens_with_scopes(sublime.Region(0, len(v)))

        kept_tokens = filter(lambda t: not sublime.score_selector(t[1], skip_scope), tokens)
        kept_text = "".join(map(lambda t: v.substr(t[0]), kept_tokens))

        new_view = v.window().new_file()
        new_view.assign_syntax(v.settings().get("syntax"))
        new_view.run_command("insert", {"characters": kept_text})

        # set viewport/cursor to top
        new_view.sel().clear()
        new_view.sel().add(0)
        new_view.set_viewport_position((0, 0))

keybinding

{
    "keys": ["whatever_keybinding"],
    "command": "filter_content_by_scopes",
    // only run on Markdown files
    "context": [{ "key": "selector", "operator": "equal", "operand": "text.html.markdown" }],
    "args": {
        "skip_scopes": [
            "markup.quote.markdown",
            // may add more...
        ],
    },
},
2 Likes

#3

Thank you, just what I need!

And one more question:

When can I learn about various values of

wanted_scope = "- markup.quote.markdown"

For example if I want to not export footnotes lists, or yaml header or so…

EDIT: OK I think I found starting point

https://www.sublimetext.com/docs/3/scope_naming.html#markup

0 Likes

#4

That’s the suggested naming convention which official syntaxes should hopefully follow but it’s not necessary to be followed by 3rd-party ones. To see what’s actually happening, press ctrl+alt+shift+p to show the scope at the cursor position. I personally haven’t used the built-in show_scope_name command for quite a long time since ScopeHunter is superior.

Use ScopeHunter’s command to replace the built-in one

// plugin: Scope Hunter
{ "keys": ["ctrl+alt+shift+p"], "command": "get_selection_scope" },

You may also be interested in how selector(s) is matched.

1 Like

#5

I’ve done some revisions to the plugin/keybinding in Export part of the *.md file to make it more flexible.

1 Like

#6

Thank you! This helps!

0 Likes