Class: Inkpen::Extensions::ForcedDocument
- 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.
Constant Summary collapse
- DEFAULT_TITLE_LEVEL =
Default heading level for the title (H1).
1- DEFAULT_SUBTITLE_LEVEL =
Default heading level for the subtitle (H2).
2- DEFAULT_TITLE_PLACEHOLDER =
Default placeholder text for the title heading.
"Untitled"- DEFAULT_SUBTITLE_PLACEHOLDER =
Default placeholder text for the subtitle heading.
"Add a subtitle..."
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
-
#allow_subtitle_deletion? ⇒ Boolean
Whether to allow the subtitle heading to be deleted.
-
#allow_title_deletion? ⇒ Boolean
Whether to allow the title heading to be deleted.
-
#content_expression ⇒ String
Document structure schema.
-
#name ⇒ Symbol
The unique name of this extension.
-
#subtitle? ⇒ Boolean
Whether subtitle is enabled.
-
#subtitle_level ⇒ Integer
The heading level for the subtitle (second heading).
-
#subtitle_placeholder ⇒ String
Placeholder text shown in the subtitle heading when empty.
-
#title_level ⇒ Integer
The heading level for the title (first heading).
-
#title_placeholder ⇒ String
Placeholder text shown in the title heading when empty.
-
#to_config ⇒ Hash
Convert to configuration hash for JavaScript.
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.
119 120 121 |
# File 'lib/inkpen/extensions/forced_document.rb', line 119 def allow_subtitle_deletion? .fetch(:allow_subtitle_deletion, true) end |
#allow_title_deletion? ⇒ Boolean
Whether to allow the title heading to be deleted.
110 111 112 |
# File 'lib/inkpen/extensions/forced_document.rb', line 110 def allow_title_deletion? .fetch(:allow_deletion, false) end |
#content_expression ⇒ String
Document structure schema.
Defines the required structure:
-
Without subtitle: heading block*
-
With subtitle: heading heading? block*
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 |
#name ⇒ Symbol
The unique name of this extension.
56 57 58 |
# File 'lib/inkpen/extensions/forced_document.rb', line 56 def name :forced_document end |
#subtitle? ⇒ Boolean
Whether subtitle is enabled.
83 84 85 |
# File 'lib/inkpen/extensions/forced_document.rb', line 83 def subtitle? .fetch(:subtitle, false) end |
#subtitle_level ⇒ Integer
The heading level for the subtitle (second heading).
74 75 76 |
# File 'lib/inkpen/extensions/forced_document.rb', line 74 def subtitle_level .fetch(:subtitle_level, DEFAULT_SUBTITLE_LEVEL) end |
#subtitle_placeholder ⇒ String
Placeholder text shown in the subtitle heading when empty.
101 102 103 |
# File 'lib/inkpen/extensions/forced_document.rb', line 101 def subtitle_placeholder .fetch(:subtitle_placeholder, DEFAULT_SUBTITLE_PLACEHOLDER) end |
#title_level ⇒ Integer
The heading level for the title (first heading).
65 66 67 |
# File 'lib/inkpen/extensions/forced_document.rb', line 65 def title_level .fetch(:title_level, .fetch(:heading_level, DEFAULT_TITLE_LEVEL)) end |
#title_placeholder ⇒ String
Placeholder text shown in the title heading when empty.
92 93 94 |
# File 'lib/inkpen/extensions/forced_document.rb', line 92 def title_placeholder .fetch(:title_placeholder, .fetch(:placeholder, DEFAULT_TITLE_PLACEHOLDER)) end |
#to_config ⇒ Hash
Convert to configuration hash for JavaScript.
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 |