Action

Add to flomo

Posted by geekdada, Last update 4 months ago

支持将 draft 导入到 flomo。需先部署 flomo-api-helper

Steps

  • script

    // See online documentation for examples
    // https://docs.getdrafts.com/docs/actions/scripting
    
    async function main() {
      const { url, apiToken } = getCredential();
      const format = getFormat();
    
      if (!url || !apiToken) {
        app.displayErrorMessage('没有配置 API 信息');
        context.cancel();
        return;
      }
    
      if (!format) {
        context.cancel();
        return;
      }
    
      if (!draft.content) {
        app.displayErrorMessage('内容为空');
        context.cancel();
        return;
      }
    
      const content =
        format === 'markdown' ? parseMd(draft.content) : draft.content;
      const contentType = format === 'markdown' ? 'html' : 'text';
    
      const message = putMemo(url, apiToken, content, contentType);
    
      app.displaySuccessMessage(message);
    }
    
    function putMemo(url, apiToken, content, contentType) {
      if (!url.endsWith('/')) {
        url += '/';
      }
    
      const tags = draft.tags ? draft.tags : [];
      const http = HTTP.create();
    
      tags.push('Drafts');
    
      const response = http.request({
        url: url + 'helper/memo',
        method: 'PUT',
        encoding: 'json',
        data: {
          content,
          contentType,
          tags: tags.join(','),
        },
        headers: {
          'X-Token': apiToken,
        },
      });
    
      if (response.success) {
        return '已创建';
      } else {
        const err = new Error(response.error);
        err.response = JSON.stringify(response);
        throw err;
      }
    }
    
    function parseMd(inputString) {
      const mmd = MultiMarkdown.create();
      mmd.format = 'html';
    
      return mmd.render(inputString);
    }
    
    function getFormat() {
      const prompt = Prompt.create();
      prompt.title = '请选择内容格式';
    
      prompt.addButton('Text', 'text');
      prompt.addButton('Markdown', 'markdown');
      prompt.show();
    
      return prompt.buttonPressed;
    }
    
    function getCredential() {
      const credential = Credential.create('Add to flomo', '输入 API 信息');
      credential.addTextField('url', 'URL');
      credential.addPasswordField('apiToken', 'API Token');
      credential.authorize();
    
      return {
        url: credential.getValue('url'),
        apiToken: credential.getValue('apiToken'),
      };
    }
    
    main()
      .then(() => script.complete())
      .catch((err) => {
        console.log(err);
        context.fail(err.message);
        script.complete();
      });
    

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.