Code Monkey home page Code Monkey logo

Comments (4)

W1ndys avatar W1ndys commented on August 14, 2024 1

上次的位置已修改

from typora_plugin.

obgnail avatar obgnail commented on August 14, 2024

@W1ndys

给一些建议:

  1. 如果你的 Typora_plugin 升级到最新版本,可以使用内置的 this.utils.editCurrentFile 函数

    class utils{
        static editCurrentFile = async (editFileFunc, reloadTypora = true) => {
            const filepath = this.getFilePath();
            const content = await this.Package.Fs.promises.readFile(filepath, "utf-8");
            const replacedContent = editFileFunc(content);
            await this.Package.Fs.promises.writeFile(filepath, replacedContent);
            reloadTypora && File.reloadContent(replacedContent, {fromDiskChange: false});
        }
    }

    使用上述方法,将其改造为:

    class MarkdownToCode extends BaseCustomPlugin {
        callback = async anchorNode => await this.utils.editCurrentFile(this.convertMarkdownToCode)
    
        // 主要逻辑代码
        convertMarkdownToCode = content => {
    
        }
    }
    
    module.exports = { plugin: MarkdownToCode };
  2. 如果将语言放在配置中,限制有点死。如果你愿意的话,可以使用 this.utils.modal 弹出一个弹窗,填写目标语言(将 this.config.code 作为默认语言),这样可以支持任意语言:

    class MarkdownToCode extends BaseCustomPlugin {
        callback = async anchorNode => await this.utils.editCurrentFile(this.convertMarkdownToCode)
    
        // 主要逻辑代码
        convertMarkdownToCode = content => {
            const components = [{label: "语言", type: "input", value: this.config.code.toLowerCase()}];
            this.utils.modal({title: "添加语言", components}, ([{submit: targetLang}]) => {
                if (!targetLang) return;
    
                alert("your target language is:", targetLang)
                // TODO
            })
        }
    }
  3. 如果你在右下角添加了按钮,如果你愿意的话,可以使用 hide 参数在右键菜单中隐藏此插件,或者使用 order 参数修改在右键菜单中的出现顺序:

    [MarkdownToCode]
    name = "标注全部代码块"  # 右键菜单中展示的名称
    enable = true         # 是否启用此自定义插件
    hide = true           # 是否在右键菜单中隐藏
    order = 1             # 在右键菜单中的出现顺序(越大越排到后面,允许负数)
    
        [MarkdownToCode.config]
        code = "python" ##这里引号内改为你想要格式化的编程语言

    注:name、enable、hide、order 是所有自定义插件都支持的参数,其中前两个必填,后两个选填(默认值分别为 false 和 1)

  4. 上次的 ReplaceBackslash,hotkey 方法放错位置了,应该为:

    class ReplaceBackslash extends BaseCustomPlugin {
        // hotkey 作为插件类的方法,返回 Array<string> 类型
        // 为什么返回数组?答:支持为同一个功能绑定多个快捷键
        hotkey = () => [this.config.hotkey]
    
        callback = async anchorNode => {
            const filepath = this.utils.getFilePath();
            ...
        }
    }

from typora_plugin.

W1ndys avatar W1ndys commented on August 14, 2024

@obgnail 谢谢大佬,理解了,我会重新修改内容

from typora_plugin.

W1ndys avatar W1ndys commented on August 14, 2024

本帖已修改

from typora_plugin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.