markly 0.14.1 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/lib/markly/node.rb +6 -7
- data/lib/markly/renderer/headings.rb +81 -0
- data/lib/markly/renderer/html.rb +12 -9
- data/lib/markly/version.rb +1 -1
- data/readme.md +6 -0
- data/releases.md +4 -0
- data.tar.gz.sig +0 -0
- metadata +2 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d9b96430051a815282d6f83d487fc0c0cdc636b174a1f1f775f0574950e8bab3
|
|
4
|
+
data.tar.gz: d16b1c808df1030e6bcc9c67c99f88a0eeae8d8e604b68745e0d8f4c16ac171d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9e7e24631dbd95cb90ee889919be9bb6a267c6e2cf31e7355dd21f5e0c903a83a288178bffcf4430eed4ed58b3d2c014e8e3617629a9c703f063069a03cfeaf7
|
|
7
|
+
data.tar.gz: 2c51b26c4ce3d2467079ce968f0d2bae307090b49b59b0acfb7396ad00c140e276f76d9291ac4bc65e24e368005b6929990521de4749fecd678f1b6b0ed6d77d
|
checksums.yaml.gz.sig
CHANGED
|
Binary file
|
data/lib/markly/node.rb
CHANGED
|
@@ -29,7 +29,7 @@ module Markly
|
|
|
29
29
|
|
|
30
30
|
# Public: An iterator that "walks the tree," descending into children recursively.
|
|
31
31
|
#
|
|
32
|
-
#
|
|
32
|
+
# block - A {Proc} representing the action to take for each child
|
|
33
33
|
def walk(&block)
|
|
34
34
|
return enum_for(:walk) unless block_given?
|
|
35
35
|
|
|
@@ -41,7 +41,7 @@ module Markly
|
|
|
41
41
|
|
|
42
42
|
# Public: Convert the node to an HTML string.
|
|
43
43
|
#
|
|
44
|
-
#
|
|
44
|
+
# flags - A {Symbol} or {Array of Symbol}s indicating the render options
|
|
45
45
|
# extensions - An {Array of Symbol}s indicating the extensions to use
|
|
46
46
|
#
|
|
47
47
|
# Returns a {String}.
|
|
@@ -51,7 +51,7 @@ module Markly
|
|
|
51
51
|
|
|
52
52
|
# Public: Convert the node to a CommonMark string.
|
|
53
53
|
#
|
|
54
|
-
#
|
|
54
|
+
# flags - A {Symbol} or {Array of Symbol}s indicating the render options
|
|
55
55
|
# width - Column to wrap the output at
|
|
56
56
|
#
|
|
57
57
|
# Returns a {String}.
|
|
@@ -63,7 +63,7 @@ module Markly
|
|
|
63
63
|
|
|
64
64
|
# Public: Convert the node to a plain text string.
|
|
65
65
|
#
|
|
66
|
-
#
|
|
66
|
+
# flags - A {Symbol} or {Array of Symbol}s indicating the render options
|
|
67
67
|
# width - Column to wrap the output at
|
|
68
68
|
#
|
|
69
69
|
# Returns a {String}.
|
|
@@ -106,7 +106,6 @@ module Markly
|
|
|
106
106
|
|
|
107
107
|
# Replace a section (header + content) with a new node.
|
|
108
108
|
#
|
|
109
|
-
# @parameter title [String] the title of the section to replace.
|
|
110
109
|
# @parameter new_node [Markly::Node] the node to replace the section with.
|
|
111
110
|
# @parameter replace_header [Boolean] whether to replace the header itself or not.
|
|
112
111
|
# @parameter remove_subsections [Boolean] whether to remove subsections or not.
|
|
@@ -132,7 +131,7 @@ module Markly
|
|
|
132
131
|
|
|
133
132
|
# Append the given node after the current node.
|
|
134
133
|
#
|
|
135
|
-
# It's okay to provide a document node,
|
|
134
|
+
# It's okay to provide a document node, its children will be appended.
|
|
136
135
|
#
|
|
137
136
|
# @parameter node [Markly::Node] the node to append.
|
|
138
137
|
def append_after(node)
|
|
@@ -151,7 +150,7 @@ module Markly
|
|
|
151
150
|
|
|
152
151
|
# Append the given node before the current node.
|
|
153
152
|
#
|
|
154
|
-
# It's okay to provide a document node,
|
|
153
|
+
# It's okay to provide a document node, its children will be appended.
|
|
155
154
|
#
|
|
156
155
|
# @parameter node [Markly::Node] the node to append.
|
|
157
156
|
def append_before(node)
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Released under the MIT License.
|
|
4
|
+
# Copyright, 2025, by Samuel Williams.
|
|
5
|
+
|
|
6
|
+
module Markly
|
|
7
|
+
module Renderer
|
|
8
|
+
# Extracts headings from a markdown document with unique anchor IDs.
|
|
9
|
+
# Handles duplicate heading text by appending counters (e.g., "deployment", "deployment-2", "deployment-3").
|
|
10
|
+
class Headings
|
|
11
|
+
def initialize
|
|
12
|
+
@ids = {}
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# Generate a unique anchor for a node.
|
|
16
|
+
# @parameter node [Markly::Node] The heading node
|
|
17
|
+
# @returns [String] A unique anchor ID
|
|
18
|
+
def anchor_for(node)
|
|
19
|
+
base = base_anchor_for(node)
|
|
20
|
+
|
|
21
|
+
if @ids.key?(base)
|
|
22
|
+
@ids[base] += 1
|
|
23
|
+
"#{base}-#{@ids[base]}"
|
|
24
|
+
else
|
|
25
|
+
@ids[base] = 1
|
|
26
|
+
base
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Extract all headings from a document root with unique anchors.
|
|
31
|
+
# @parameter root [Markly::Node] The document root node
|
|
32
|
+
# @parameter min_level [Integer] Minimum heading level to extract (default: 1)
|
|
33
|
+
# @parameter max_level [Integer] Maximum heading level to extract (default: 6)
|
|
34
|
+
# @returns [Array<Heading>] Array of heading objects with unique anchors
|
|
35
|
+
def extract(root, min_level: 1, max_level: 6)
|
|
36
|
+
headings = []
|
|
37
|
+
root.walk do |node|
|
|
38
|
+
if node.type == :header
|
|
39
|
+
level = node.header_level
|
|
40
|
+
next if level < min_level || level > max_level
|
|
41
|
+
|
|
42
|
+
headings << Heading.new(
|
|
43
|
+
node: node,
|
|
44
|
+
level: level,
|
|
45
|
+
text: node.to_plaintext.chomp,
|
|
46
|
+
anchor: anchor_for(node)
|
|
47
|
+
)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
headings
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Class method for convenience - creates a new instance and extracts headings.
|
|
54
|
+
# @parameter root [Markly::Node] The document root node
|
|
55
|
+
# @parameter min_level [Integer] Minimum heading level to extract (default: 1)
|
|
56
|
+
# @parameter max_level [Integer] Maximum heading level to extract (default: 6)
|
|
57
|
+
# @returns [Array<Heading>] Array of heading objects with unique anchors
|
|
58
|
+
def self.extract(root, min_level: 1, max_level: 6)
|
|
59
|
+
new.extract(root, min_level: min_level, max_level: max_level)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
# Generate a base anchor from a node's text content.
|
|
65
|
+
# @parameter node [Markly::Node] The heading node
|
|
66
|
+
# @returns [String] The base anchor (lowercase, hyphenated)
|
|
67
|
+
def base_anchor_for(node)
|
|
68
|
+
text = node.to_plaintext.chomp.downcase
|
|
69
|
+
text.gsub(/\s+/, "-")
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Represents a heading extracted from a document.
|
|
74
|
+
# @attribute node [Markly::Node] The original heading node
|
|
75
|
+
# @attribute level [Integer] The heading level (1-6)
|
|
76
|
+
# @attribute text [String] The plain text content of the heading
|
|
77
|
+
# @attribute anchor [String] The unique anchor ID for this heading
|
|
78
|
+
Heading = Struct.new(:node, :level, :text, :anchor, keyword_init: true)
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
|
data/lib/markly/renderer/html.rb
CHANGED
|
@@ -9,15 +9,18 @@
|
|
|
9
9
|
# Copyright, 2020-2025, by Samuel Williams.
|
|
10
10
|
|
|
11
11
|
require_relative "generic"
|
|
12
|
+
require_relative "headings"
|
|
12
13
|
require "cgi"
|
|
13
14
|
|
|
14
15
|
module Markly
|
|
15
16
|
module Renderer
|
|
16
17
|
class HTML < Generic
|
|
17
|
-
def initialize(ids: false, tight: false, **options)
|
|
18
|
+
def initialize(ids: false, headings: nil, tight: false, **options)
|
|
18
19
|
super(**options)
|
|
19
20
|
|
|
20
|
-
|
|
21
|
+
# Initialize heading tracker if IDs are enabled
|
|
22
|
+
@headings = headings || (ids ? Headings.new : nil)
|
|
23
|
+
|
|
21
24
|
@section = nil
|
|
22
25
|
@tight = tight
|
|
23
26
|
|
|
@@ -32,8 +35,8 @@ module Markly
|
|
|
32
35
|
end
|
|
33
36
|
|
|
34
37
|
def id_for(node)
|
|
35
|
-
if @
|
|
36
|
-
anchor =
|
|
38
|
+
if @headings
|
|
39
|
+
anchor = @headings.anchor_for(node)
|
|
37
40
|
return " id=\"#{CGI.escape_html anchor}\""
|
|
38
41
|
end
|
|
39
42
|
end
|
|
@@ -54,7 +57,7 @@ module Markly
|
|
|
54
57
|
|
|
55
58
|
def header(node)
|
|
56
59
|
block do
|
|
57
|
-
if @
|
|
60
|
+
if @headings
|
|
58
61
|
out("</section>") if @section
|
|
59
62
|
@section = true
|
|
60
63
|
out("<section#{id_for(node)}>")
|
|
@@ -253,10 +256,10 @@ module Markly
|
|
|
253
256
|
end
|
|
254
257
|
|
|
255
258
|
TABLE_CELL_ALIGNMENT = {
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
259
|
+
left: ' align="left"',
|
|
260
|
+
right: ' align="right"',
|
|
261
|
+
center: ' align="center"'
|
|
262
|
+
}.freeze
|
|
260
263
|
|
|
261
264
|
def table_cell(node)
|
|
262
265
|
align = TABLE_CELL_ALIGNMENT.fetch(@alignments[@column_index], "")
|
data/lib/markly/version.rb
CHANGED
data/readme.md
CHANGED
|
@@ -22,10 +22,16 @@ Please see the [project documentation](https://ioquatix.github.io/markly/) for m
|
|
|
22
22
|
|
|
23
23
|
- [Abstract Syntax Tree](https://ioquatix.github.io/markly/guides/abstract-syntax-tree/index) - This guide explains how to use Markly's abstract syntax tree (AST) to parse and manipulate Markdown documents.
|
|
24
24
|
|
|
25
|
+
- [Headings](https://ioquatix.github.io/markly/guides/headings/index) - This guide explains how to work with headings in Markly, including extracting them for navigation and handling duplicate heading text.
|
|
26
|
+
|
|
25
27
|
## Releases
|
|
26
28
|
|
|
27
29
|
Please see the [project releases](https://ioquatix.github.io/markly/releases/index) for all releases.
|
|
28
30
|
|
|
31
|
+
### v0.15.0
|
|
32
|
+
|
|
33
|
+
- Introduced `Markly::Renderer::Headings` class for extracting headings from markdown documents with automatic duplicate ID resolution. When rendering HTML with `ids: true`, duplicate heading text now automatically gets unique IDs (`deployment`, `deployment-2`, `deployment-3`). The `Headings` class can also be used to extract headings for building navigation or table of contents.
|
|
34
|
+
|
|
29
35
|
### v0.14.0
|
|
30
36
|
|
|
31
37
|
- Expose `Markly::Renderer::HTML.anchor_for` method to generate URL-safe anchors from headers.
|
data/releases.md
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# Releases
|
|
2
2
|
|
|
3
|
+
## v0.15.0
|
|
4
|
+
|
|
5
|
+
- Introduced `Markly::Renderer::Headings` class for extracting headings from markdown documents with automatic duplicate ID resolution. When rendering HTML with `ids: true`, duplicate heading text now automatically gets unique IDs (`deployment`, `deployment-2`, `deployment-3`). The `Headings` class can also be used to extract headings for building navigation or table of contents.
|
|
6
|
+
|
|
3
7
|
## v0.14.0
|
|
4
8
|
|
|
5
9
|
- Expose `Markly::Renderer::HTML.anchor_for` method to generate URL-safe anchors from headers.
|
data.tar.gz.sig
CHANGED
|
Binary file
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: markly
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.15.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Garen Torikian
|
|
@@ -138,6 +138,7 @@ files:
|
|
|
138
138
|
- lib/markly/node.rb
|
|
139
139
|
- lib/markly/node/inspect.rb
|
|
140
140
|
- lib/markly/renderer/generic.rb
|
|
141
|
+
- lib/markly/renderer/headings.rb
|
|
141
142
|
- lib/markly/renderer/html.rb
|
|
142
143
|
- lib/markly/version.rb
|
|
143
144
|
- license.md
|
metadata.gz.sig
CHANGED
|
Binary file
|