Bookmark Node Type

Category: Custom • Detailed implementation guide with view integration notes

Schema Definition

Schema definition for the Bookmark node type:

{
  bookmark: {
    group: 'block',
    attrs: {
      url: { default: '' },
      title: { default: '' },
      description: { default: '' },
      image: { default: null }
    },
  }
}

Model Representation

Example model representation:

{
  type: 'bookmark',
  attrs: {
    url: 'https://example.com',
    title: 'Example Website',
    description: 'An example website',
    image: 'https://example.com/image.jpg'
  }
}

HTML Serialization

Converting model to HTML:

function serializeBookmark(node) {
  return '<div data-type="bookmark" data-url="' + escapeHtml(node.attrs.url) + 
         '" data-title="' + escapeHtml(node.attrs.title) + 
         '" data-description="' + escapeHtml(node.attrs.description) + 
         '" data-image="' + (node.attrs.image ? escapeHtml(node.attrs.image) : '') + '"></div>';
}

HTML Deserialization

Parsing HTML to model:

function parseBookmark(domNode) {
  return {
    type: 'bookmark',
    attrs: {
      url: domNode.getAttribute('data-url') || '',
      title: domNode.getAttribute('data-title') || '',
      description: domNode.getAttribute('data-description') || '',
      image: domNode.getAttribute('data-image') || null
    }
  };
}

View Integration

View Integration Notes: Pay special attention to contenteditable behavior, selection handling, and event management when implementing this node type in your view layer.

View integration code:

// Rendering bookmark
const bookmark = document.createElement('div');
bookmark.setAttribute('data-type', 'bookmark');
bookmark.setAttribute('data-url', node.attrs.url);
bookmark.setAttribute('data-title', node.attrs.title);
bookmark.setAttribute('data-description', node.attrs.description);
if (node.attrs.image) bookmark.setAttribute('data-image', node.attrs.image);
bookmark.contentEditable = 'false'; // Bookmarks are typically rendered

Common Issues

Common Pitfalls: These are issues frequently encountered when implementing this node type. Review carefully before implementation.

Common issues and solutions:

// Issue: Bookmark metadata fetching
// Solution: Fetch Open Graph metadata
async function fetchBookmarkMetadata(url) {
  // Fetch URL and extract Open Graph tags
  const response = await fetch(url);
  const html = await response.text();
  // Parse Open Graph metadata
  return extractOpenGraphData(html);
}

// Issue: Bookmark URL validation
// Solution: Validate URL format
function validateBookmarkUrl(url) {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
}

Implementation

Complete implementation example:

class BookmarkNode {
  constructor(attrs) {
    this.type = 'bookmark';
    this.attrs = {
      url: attrs?.url || '',
      title: attrs?.title || '',
      description: attrs?.description || '',
      image: attrs?.image || null
    };
  }
  
  toDOM() {
    const bookmark = document.createElement('div');
    bookmark.setAttribute('data-type', 'bookmark');
    bookmark.setAttribute('data-url', this.attrs.url);
    bookmark.setAttribute('data-title', this.attrs.title);
    bookmark.setAttribute('data-description', this.attrs.description);
    if (this.attrs.image) bookmark.setAttribute('data-image', this.attrs.image);
    return bookmark;
  }
  
  static fromDOM(domNode) {
    return new BookmarkNode({
      url: domNode.getAttribute('data-url') || '',
      title: domNode.getAttribute('data-title') || '',
      description: domNode.getAttribute('data-description') || '',
      image: domNode.getAttribute('data-image') || null
    });
  }
}