Action

Split to Ulysses

Posted by RoyRogers, Last update over 4 years ago

Split Markdown text to sheets in Ulysses

  • Splits text blocks by Markdown heading levels and
  • exports them to Ulysses group as multiple sheets
  • in correct order (manual sort in Ulysses).

Steps

  • script

    // Split Markdown text to sheets in Ulysses
    // RV 2020-03-14 at 20:00 EST
    
    // Splits text blocks by Markdown heading levels and exports 
    // them to Ulysses group as multiple sheets in correct order.
    // (manual sort in Ulysses)
    
    main: {
      let level = SelectHeadingLevel();
      if (!level) { 
        // cancel prompt to break loop
        context.cancel('Canceled');
        break main;
      }
      let text = draft.content;
      //let title = text.match(/^#* ?(.+?)$/m)[1]
      
      let repl = '\\n(?=#{1,'+level+'} )';
      let re = new RegExp(repl,"g");
      let blocks = text.split(re);
      
      let group = draft.processTemplate('[[safe_title]]')
      
      if (!MakeUlyssesGroup(group)) {
        context.cancel('Canceled');
        alert('Failed group creation!')
        break main;
      }
      
      for (const text of blocks.reverse()) {
        //	editor.arrange(block);
        if (!MakeUlyssesSheet(group, text)) {
          context.cancel('Canceled');
          alert('Failed Sheet creation!')
          break main;
        }
      }
    }
    
    function MakeUlyssesSheet(group, text) {
      const baseURL = 'ulysses://x-callback-url/new-sheet?index=0';
      let cb = CallbackURL.create();
      cb.baseURL = baseURL;
      cb.addParameter("group", group);
      cb.addParameter("text", text);
      cb.waitForResponse = true;
      let success = cb.open();
      if (success) {
        return true;
      }
      return false;
    }
    
    function MakeUlyssesGroup(name) {
      const baseURL = 'ulysses://x-callback-url/new-group?index=1';
      let cb = CallbackURL.create();
      cb.baseURL = baseURL;
      cb.addParameter("name", name);
      cb.waitForResponse = true;
      let success = cb.open();
      if (success) {
        return true;
      }
      return false;
    }
    
    function SelectHeadingLevel() {
      let p = Prompt.create();
      p.title = "Markdown Split to Ulysses";
      p.message = "Splits on heading level:";
      
      let options = ["H1: #", "H2: ##", "H3: ###", "H4: ####"];
      let selected = ["H2: ##"];
      
      // single selection
      p.addSelect("s1", "Select heading level...", options, selected, false);
      p.addButton("OK");
      
      if (!p.show()) {
        // Prompt 'Cancel'
        return false;
      }
      // Prompt 'OK'
      return String(p.fieldValues["s1"])[1]; // 2nd char: the number only.
    }
    
    
  • url

    template
    ulysses://
    useSafari
    false
    encodeTags
    true

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.