Class: Inkpen::Extensions::ForcedDocument

Inherits:
Base
  • Object
show all
Defined in:
lib/inkpen/extensions/forced_document.rb

Overview

Forced Document Structure extension.

Enforces a specific document structure where the first element must be a title heading (H1), optionally followed by a subtitle heading (H2). This prevents users from deleting required headings and ensures consistent document structure similar to Medium, Notion, or Dropbox Paper.

Examples:

Basic usage (title only)

extension = Inkpen::Extensions::ForcedDocument.new

With subtitle enabled

extension = Inkpen::Extensions::ForcedDocument.new(
  subtitle: true,
  title_placeholder: "Your title here...",
  subtitle_placeholder: "Add a subtitle..."
)

Custom heading levels

extension = Inkpen::Extensions::ForcedDocument.new(
  title_level: 1,
  subtitle_level: 2,
  subtitle: true
)

See Also:

Author:

  • Inkpen Team

Since:

  • 0.2.0

Constant Summary collapse

DEFAULT_TITLE_LEVEL =

Default heading level for the title (H1).

Since:

  • 0.2.0

1
DEFAULT_SUBTITLE_LEVEL =

Default heading level for the subtitle (H2).

Since:

  • 0.2.0

2
DEFAULT_TITLE_PLACEHOLDER =

Default placeholder text for the title heading.

Since:

  • 0.2.0

"Untitled"
DEFAULT_SUBTITLE_PLACEHOLDER =

Default placeholder text for the subtitle heading.

Since:

  • 0.2.0

"Add a subtitle..."

Instance Attribute Summary

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#enabled?, #initialize, #to_h, #to_json

Constructor Details

This class inherits a constructor from Inkpen::Extensions::Base

Instance Method Details

#allow_subtitle_deletion?Boolean

Whether to allow the subtitle heading to be deleted.

Returns:

  • (Boolean)

    true by default (subtitle can be deleted/cleared)

Since:

  • 0.2.0



119
120
121
# File 'lib/inkpen/extensions/forced_document.rb', line 119

def allow_subtitle_deletion?
  options.fetch(:allow_subtitle_deletion, true)
end

#allow_title_deletion?Boolean

Whether to allow the title heading to be deleted.

Returns:

  • (Boolean)

    false by default (title cannot be deleted)

Since:

  • 0.2.0



110
111
112
# File 'lib/inkpen/extensions/forced_document.rb', line 110

def allow_title_deletion?
  options.fetch(:allow_deletion, false)
end

#content_expressionString

Document structure schema.

Defines the required structure:

  • Without subtitle: heading block*

  • With subtitle: heading heading? block*

Returns:

  • (String)

    ProseMirror content expression

Since:

  • 0.2.0



132
133
134
135
136
137
138
# File 'lib/inkpen/extensions/forced_document.rb', line 132

def content_expression
  if subtitle?
    "heading heading? block*"
  else
    "heading block*"
  end
end

#nameSymbol

The unique name of this extension.

Returns:

  • (Symbol)

    :forced_document

Since:

  • 0.2.0



56
57
58
# File 'lib/inkpen/extensions/forced_document.rb', line 56

def name
  :forced_document
end

#subtitle?Boolean

Whether subtitle is enabled.

Returns:

  • (Boolean)

    true if subtitle heading should be enforced

Since:

  • 0.2.0



83
84
85
# File 'lib/inkpen/extensions/forced_document.rb', line 83

def subtitle?
  options.fetch(:subtitle, false)
end

#subtitle_levelInteger

The heading level for the subtitle (second heading).

Returns:

  • (Integer)

    heading level (1-6), default 2

Since:

  • 0.2.0



74
75
76
# File 'lib/inkpen/extensions/forced_document.rb', line 74

def subtitle_level
  options.fetch(:subtitle_level, DEFAULT_SUBTITLE_LEVEL)
end

#subtitle_placeholderString

Placeholder text shown in the subtitle heading when empty.

Returns:

  • (String)

    placeholder text

Since:

  • 0.2.0



101
102
103
# File 'lib/inkpen/extensions/forced_document.rb', line 101

def subtitle_placeholder
  options.fetch(:subtitle_placeholder, DEFAULT_SUBTITLE_PLACEHOLDER)
end

#title_levelInteger

The heading level for the title (first heading).

Returns:

  • (Integer)

    heading level (1-6), default 1

Since:

  • 0.2.0



65
66
67
# File 'lib/inkpen/extensions/forced_document.rb', line 65

def title_level
  options.fetch(:title_level, options.fetch(:heading_level, DEFAULT_TITLE_LEVEL))
end

#title_placeholderString

Placeholder text shown in the title heading when empty.

Returns:

  • (String)

    placeholder text

Since:

  • 0.2.0



92
93
94
# File 'lib/inkpen/extensions/forced_document.rb', line 92

def title_placeholder
  options.fetch(:title_placeholder, options.fetch(:placeholder, DEFAULT_TITLE_PLACEHOLDER))
end

#to_configHash

Convert to configuration hash for JavaScript.

Returns:

  • (Hash)

    configuration for the TipTap extension

Since:

  • 0.2.0



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/inkpen/extensions/forced_document.rb', line 145

def to_config
  config = {
    titleLevel: title_level,
    titlePlaceholder: title_placeholder,
    allowDeletion: allow_title_deletion?,
    contentExpression: content_expression,
    subtitle: subtitle?
  }

  if subtitle?
    config.merge!(
      subtitleLevel: subtitle_level,
      subtitlePlaceholder: subtitle_placeholder,
      allowSubtitleDeletion: allow_subtitle_deletion?
    )
  end

  # Legacy support for headingLevel
  config[:headingLevel] = title_level

  config
end