diff --git a/web/cm_plugins/link.ts b/web/cm_plugins/link.ts index 08027c7..ee351c7 100644 --- a/web/cm_plugins/link.ts +++ b/web/cm_plugins/link.ts @@ -34,6 +34,17 @@ export function linkPlugin(editor: Editor) { if (isCursorInRange(view.state, [from, to])) { return; } + + const text = view.state.sliceDoc(from, to); + // Links are of the form [hell](https://example.com) + const [anchorPart, linkPart] = text.split("]("); // Not pretty + if (!linkPart) { + // Invalid link + return; + } + const cleanAnchor = anchorPart.substring(1); // cut off the initial [ + const cleanLink = linkPart.substring(0, linkPart.length - 1); // cut off the final ) + // Hide the whole thing widgets.push( invisibleDecoration.range( @@ -42,12 +53,6 @@ export function linkPlugin(editor: Editor) { ), ); - const text = view.state.sliceDoc(from, to); - // Links are of the form [hell](https://example.com) - const [anchorPart, linkPart] = text.split("]("); // Not pretty - const cleanAnchor = anchorPart.substring(1); // cut off the initial [ - const cleanLink = linkPart.substring(0, linkPart.length - 1); // cut off the final ) - widgets.push( Decoration.widget({ widget: new LinkWidget( diff --git a/web/editor.tsx b/web/editor.tsx index 65a9779..8eb557a 100644 --- a/web/editor.tsx +++ b/web/editor.tsx @@ -304,8 +304,8 @@ export class Editor { let initialCursorPos = 0; const match = frontMatterRegex.exec(pageText); if (match) { - // Frotnmatter found, put cursor after it - initialCursorPos = match[0].length; + // Frontmatter found, put cursor after it + initialCursorPos = match[0].length + 1; } // By default scroll to the top this.editorView.scrollDOM.scrollTop = 0;