Files
plainleaf/libraries/Library/Std/Infrastructure/Export.md
T
Zef Hemel 26d9d5bdf0 Restructure standard library and improve documentation
Refactor Library/Std into subfolders for APIs, Infrastructure, Editor,
Widgets, etc. Add more documentation and descriptions. Update references
throughout website and docs.
2025-10-01 11:22:31 +02:00

2.6 KiB

description, tags
description tags
Implements the infrastructure of the Export commands. meta

Provides infrastructure for exporting pages outside of your space. It standardizes the ${widgets.commandButton("Export: Page Or Selection")} to export the current page or selection in various ways.

Architecture

Flow:

  1. Emit event export:discover with:
  • pageMeta
  • selection text
  1. Respond with:
  • id: Call identifier
  • name
  1. Ask user to select option, then invoke export:run:${id} with the same data as step 1
  2. Exporter listens to this event and handles it as it sees fit.

Implementation

command.define {
  name = "Export: Page Or Selection",
  key = "Ctrl-e",
  mac = "Cmd-e",
  run = function()
    editor.save()
    local text = editor.getText()
    local meta = editor.getCurrentPageMeta()
    local selection = editor.getSelection()
    local exportObj = {
      pageMeta = meta,
      text = selection.text != "" and selection.text or text
    }
    local handlers = event.dispatch("export:discover", exportObj)
    if #handlers == 0 then
      editor.flashNotification("No exporters available", "error")
      return
    end
    local selectionOptions = {}
    for _, handler in ipairs(handlers) do
      for _, option in ipairs(handler) do
        table.insert(selectionOptions, option)
      end
    end
    local selectedOption = editor.filterBox("Export to",
      selectionOptions, "Select your export mechanism")
    if not selectedOption then
      return
    end
    event.dispatch("export:run:" .. selectedOption.id, exportObj)
  end
}

Clipboard exporters

Implements two exporters:

  • Copy clean markdown
  • Copy rich text (e.g. for pasting into a Google Docs)
event.listen {
  name = "export:discover",
  run = function(event)
    return {
      {
        id = "clipboard-clean-markdown",
        name = "Clipboard as clean markdown"
      },
      {
        id = "clipboard-rich-text",
        name = "Clipboard as rich text"
      },
    }
  end
}

event.listen {
  name = "export:run:clipboard-clean-markdown",
  run = function(event)
    local mdTree = markdown.parseMarkdown(event.data.text)
    mdTree = markdown.expandMarkdown(mdTree)
    local renderedMd = markdown.renderParseTree(mdTree)
    editor.copyToClipboard(renderedMd)
  end
}

event.listen {
  name = "export:run:clipboard-rich-text",
  run = function(event)
    local mdTree = markdown.parseMarkdown(event.data.text)
    mdTree = markdown.expandMarkdown(mdTree)
    local html = markdown.markdownToHtml(markdown.renderParseTree(mdTree))
    editor.copyToClipboard(js.new(js.window.Blob, {html}, {type="text/html"}))
  end
}