Skip to content

Commit

Permalink
Merge pull request #33 from aguingand/improve-parse-perf
Browse files Browse the repository at this point in the history
Improve parse performance
  • Loading branch information
aguingand authored Jul 22, 2023
2 parents 3f6f8c2 + 2a39cf2 commit 388604b
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 14 deletions.
3 changes: 2 additions & 1 deletion example/src/components/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
import CodeBlock from "@tiptap/extension-code-block";
import { Markdown } from "tiptap-markdown";
import MenuBar from "./MenuBar.vue";
import content from '../content.md?raw';
import content from '../data/content.md?raw';
// import content from "../data/large-content";
import Highlight from "../extensions/highlight";
import Container from "../extensions/container";
Expand Down
File renamed without changes.
77 changes: 77 additions & 0 deletions example/src/data/large-content.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Markdown.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const Markdown = Extension.create({
onBeforeCreate() {
this.editor.storage.markdown = {
options: { ...this.options },
parser: new MarkdownParser(this.editor),
parser: new MarkdownParser(this.editor, this.options),
serializer: new MarkdownSerializer(this.editor),
getMarkdown: () => {
return this.editor.storage.markdown.serializer.serialize(this.editor.state.doc);
Expand Down
21 changes: 10 additions & 11 deletions src/parse/MarkdownParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,23 @@ export class MarkdownParser {
* @type {import('@tiptap/core').Editor}
*/
editor = null;
/**
* @type {markdownit}
*/
md = null;

constructor(editor) {
constructor(editor, { html, linkify, breaks }) {
this.editor = editor;
}

parse(content, { inline } = {}) {
const {
this.md = markdownit({
html,
linkify,
breaks,
} = this.editor.storage.markdown.options;
});
}

parse(content, { inline } = {}) {
if(typeof content === 'string') {
const renderer = markdownit({
html,
linkify,
breaks,
});
const renderer = this.md;

this.editor.extensionManager.extensions.forEach(extension =>
getMarkdownSpec(extension)?.parse?.setup?.call({ editor:this.editor, options:extension.options }, renderer)
Expand Down
4 changes: 3 additions & 1 deletion src/util/markdown.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import markdownit from 'markdown-it';

const md = markdownit();

function scanDelims(text, pos) {
const state = new (markdownit().inline.State)(text, null, null, []);
md.inline.State.prototype.scanDelims.call({ src: text, posMax: text.length })
const state = new (md.inline.State)(text, null, null, []);
return state.scanDelims(pos, true);
}

Expand Down

0 comments on commit 388604b

Please sign in to comment.