|
27 | 27 | # Track URL normalizations
|
28 | 28 | normalized_urls = {}
|
29 | 29 |
|
30 |
| -# Helper method to normalize URLs |
| 30 | +## |
| 31 | +# Normalizes a URL into a file path by removing fragments, |
| 32 | +# ensuring a leading slash, appending "index.html" for directory URLs or URLs missing an extension, |
| 33 | +# and then stripping the leading slash for file operations. |
| 34 | +# |
| 35 | +# @param url [String] the URL to be normalized. |
| 36 | +# @return [String] the normalized relative file path. |
| 37 | +# |
| 38 | +# @example |
| 39 | +# normalize_url("about") #=> "about/index.html" |
| 40 | +# normalize_url("/contact#team") #=> "contact/index.html" |
| 41 | +# normalize_url("/index.html") #=> "index.html" |
31 | 42 | def normalize_url(url)
|
32 | 43 | # Remove anchor
|
33 | 44 | url = url.split('#').first
|
@@ -119,7 +130,20 @@ def normalize_url(url)
|
119 | 130 | end
|
120 | 131 | end
|
121 | 132 |
|
122 |
| -# Function to update HTML files with metadata |
| 133 | +## |
| 134 | +# Updates an HTML file with SEO metadata by modifying its keywords and description meta tags. |
| 135 | +# |
| 136 | +# This method reads the HTML file at the specified path and uses Nokogiri to parse and update—or add—meta tags for keywords and description based on the provided collections. It concatenates unique keywords and uses the first description when appropriate, updating the meta description only if it is missing or too short. |
| 137 | +# |
| 138 | +# @param file_path [String] Path to the HTML file to update. |
| 139 | +# @param keywords [#to_a] A collection of keywords to include in the meta keywords tag. |
| 140 | +# @param description [#to_a] A collection where the first element is used for the meta description tag. |
| 141 | +# |
| 142 | +# @return [Boolean, nil] Returns true if the update was successful, false if an error occurred, or nil if the file |
| 143 | +# does not exist or its HTML lacks a head element. |
| 144 | +# |
| 145 | +# @example |
| 146 | +# update_successful = update_html_with_metadata("public/index.html", Set.new(["ruby", "seo"]), Set.new(["Enhance page visibility with Ruby"])) |
123 | 147 | def update_html_with_metadata(file_path, keywords, description)
|
124 | 148 | return unless File.exist?(file_path)
|
125 | 149 |
|
@@ -196,7 +220,15 @@ def update_html_with_metadata(file_path, keywords, description)
|
196 | 220 |
|
197 | 221 | puts "Updated #{files_updated} HTML files with SEO metadata"
|
198 | 222 |
|
199 |
| -# Generate sitemapindex.xml |
| 223 | +## |
| 224 | +# Generates a sitemap index XML file from an existing sitemap. |
| 225 | +# |
| 226 | +# This method checks for a sitemap.xml file in the specified directory. If found, it retrieves the file's last modified timestamp, |
| 227 | +# creates a sitemapindex.xml file linking to the sitemap with the last modification date, writes the file to disk, and returns true. |
| 228 | +# If sitemap.xml is not present, the method logs a message and returns false. |
| 229 | +# |
| 230 | +# @param site_dir [String] The directory containing the sitemap.xml file. |
| 231 | +# @return [Boolean] True if sitemapindex.xml is generated successfully; false otherwise. |
200 | 232 | def generate_sitemapindex(site_dir)
|
201 | 233 | sitemap_path = File.join(site_dir, 'sitemap.xml')
|
202 | 234 | sitemapindex_path = File.join(site_dir, 'sitemapindex.xml')
|
|
0 commit comments