如何使用Quill JS创建目录表格。

huangapple go评论71阅读模式
英文:

how to create table of contents with quill js

问题

有没有一种方法可以在quilljs中自动将标题标签列为目录表格(TOC)?我搜索了但找不到有用的信息。

可以使用以下代码为标题标签添加id,但如何创建链接到它们?

let Header = Quill.import('formats/header');
Header.create = function(value) {
  let header = document.createElement('h1');
  header.id = 'some-unique-id';
  return header;
};
英文:

Is there any way to automatically list header tags as a table of content (TOC) in quilljs? I searched it but couldn't find anything useful.

One can add id to header tags with the folloing code but how to create links to them?

let Header = Quill.import('formats/header');
Header.create = function(value) {
let header = document.createElement('h1');
header.id = 'some-unique-id';
  return header;
};

答案1

得分: 1

经过大量文档阅读和努力,我编写了简单、直接,但未经优化的JavaScript代码,以从Quill编辑器自动创建目录(TOC)表格。主要问题在于实现这一点需要两个方面的工作:

  • 链接默认行为,因为Quill链接模块具有target="blank"属性。因此,需要修改它以具有target="self"属性,以保留在页面中。
  • 标题标签没有id属性以创建锚链接。因此,标题模块也需要一些定制。

多亏了Quill,它具有如此高度的可定制性,以满足这些要求。

以下是链接模块的定制:

var Link = Quill.import('formats/link');

class MyLink extends Link {
    static create(value) {
        let node = Link.create(value);
        value = Link.sanitize(value);
        node.setAttribute('href', value);
        if (value.startsWith("#")) {
            node.removeAttribute('target');
        } else {
            node.setAttribute("target", "_blank");
        }
        return node;
    }

    format(name, value) {
        super.format(name, value);

        if (name !== this.statics.blotName || !value) {
            return;
        }

        if (value.startsWith("#")) {
            this.domNode.removeAttribute("target");
        } else {
            this.domNode.setAttribute("target", "_blank");
        }
    }
}

Quill.register(MyLink);

以下是定制的标题标签:

var ids = [];

function getRandomId(domNode) {
    let _id = Math.random().toString(16).slice(2, 9);
    ids.push(_id);
    return _id;
}

class MyHeader extends Header {

    constructor(domNode) {
        let a = super(domNode);
        domNode.setAttribute('id', getRandomId(domNode));
        this.cache = {};
    }

    static create(value) {
        const node = super.create();
        return node;
    }

    static formats(domNode) {
        return {
            id: domNode.getAttribute("id")
        };
    }
}

Quill.register("formats/header", MyHeader);
MyHeader.blotName = "header";

拥有这些新模块后,请注意,为了创建TOC,我们需要执行更多步骤:查找所有标题标签,为它们添加有意义的id属性,并将它们的列表放在帖子开头。

有关详细说明以及在Laravel中的完整实现,请参阅此故事,以及有关演示,请参阅此博客文章

英文:

After lots of document reading and effort, I wrote simple, straightforward and also not-optimized JavaScript codes to automatically create a table of contents (TOC) from Quill editor.
The main point is that accomplishing this needs two things:

  • Link with default behavior because Quill link module has target="_blank" attribute. So it needs to be modified to have target="_self" attribute in order to remain in the page.
  • Header tags don't have id attribute to create anchor links. Hence Header module also needs some customization.

And thanks to Quill, It is so customizable that these requirements can be implemented.

Here is the customization of Link module:

            var Link = Quill.import('formats/link');

        class MyLink extends Link {
            static create(value) {
                let node = Link.create(value);
                value = Link.sanitize(value);
                node.setAttribute('href', value);
                if (value.startsWith("#")) {
                    node.removeAttribute('target');
                } else {
                    node.setAttribute("target", "_blank");
                }
                return node;
            }

            format(name, value) {
                super.format(name, value);

                if (name !== this.statics.blotName || !value) {
                    return;
                }

                if (value.startsWith("#")) {
                    this.domNode.removeAttribute("target");
                } else {
                    this.domNode.setAttribute("target", "_blank");
                }
            }
        }

        Quill.register(MyLink);

And the following is customized header tag:

            var ids = [];

        function getRandomId(domNode) {
            let _id = Math.random().toString(16).slice(2, 9);
            ids.push(_id);
            return _id;
        }

        class MyHeader extends Header {

            constructor(domNode) {
                let a = super(domNode);
                domNode.setAttribute('id', getRandomId(domNode));
                this.cache = {};
            }

            static create(value) {
                const node = super.create();
                return node;
            }

            static formats(domNode) {
                return {
                    id: domNode.getAttribute("id")
                };
            }
        }

        Quill.register("formats/header", MyHeader);
        MyHeader.blotName = "header"

Having these new modules, note that in order to create TOC we need several more steps: finding all header tags, add some meaningful id attributes to them and put their list at the beginning of the post.

For detailed explanation and completed implementation in Laravel, look at this story and for a demo look at this blog post.

huangapple
  • 本文由 发表于 2023年6月2日 13:51:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76387450.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定