Skip to content

Commit

Permalink
changed error to graceful error
Browse files Browse the repository at this point in the history
  • Loading branch information
nartix committed Mar 26, 2024
1 parent d3bd5a5 commit 6e18d67
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nartix/tiptap-inline-code-highlight",
"version": "1.0.9",
"version": "1.0.10",
"description": "Inline code highlight extension for Tiptap editor.",
"main": "src/index.js",
"scripts": {
Expand Down
25 changes: 12 additions & 13 deletions src/code-inline-lowlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ function isFunction(param) {
return typeof param === 'function';
}

function validateLowlight(lowlight) {
const requiredMethods = ['highlight', 'highlightAuto', 'listLanguages'];
return requiredMethods.every((api) => isFunction(lowlight[api]));
}

export const CodeInlineLowlight = Extension.create({
name: 'codeInlineLowlight',

Expand All @@ -84,20 +89,18 @@ export const CodeInlineLowlight = Extension.create({
},

addProseMirrorPlugins() {
if (!['highlight', 'highlightAuto', 'listLanguages'].every((api) => isFunction(this.options.lowlight[api]))) {
throw Error('You should provide an instance of lowlight to use the @nartix/tiptap-code-inline-highlight extension');
if (!validateLowlight(this.options.lowlight)) {
console.warn(
'You should provide an instance of lowlight to use the @nartix/tiptap-code-inline-highlight extension. No syntax highlighting is applied.'
);
return [];
}
const pluginKey = new PluginKey(this.name);
return [
new Plugin({
key: pluginKey,
state: {
init: (_, { doc }) =>
getDecorations({
doc: doc,
name: CODE_MARK_TYPE,
lowlight: this.options.lowlight,
}),
init: (_, { doc }) => getDecorations({ doc, name: CODE_MARK_TYPE, lowlight: this.options.lowlight }),
apply: (tr, set, oldState, newState) => {
const oldMarks = findInlineCode(oldState.doc, CODE_MARK_TYPE);
const newMarks = findInlineCode(newState.doc, CODE_MARK_TYPE);
Expand All @@ -115,11 +118,7 @@ export const CodeInlineLowlight = Extension.create({
);
}))
) {
return getDecorations({
doc: tr.doc,
name: CODE_MARK_TYPE,
lowlight: this.options.lowlight,
});
return getDecorations({ doc: tr.doc, name: CODE_MARK_TYPE, lowlight: this.options.lowlight });
}
return set.map(tr.mapping, tr.doc);
},
Expand Down

0 comments on commit 6e18d67

Please sign in to comment.