Skip to content

Commit 74d8fe6

Browse files
Adds dynamic <title> tag based on post title (#2)
* test: Use dynamic title in posts. * docs: Describe new feature for dynamic title in posts. * feat: Dynamic title for posts. * chore: Prepare for 1.2.0 release. * docs: Link to PR, not to issue.
1 parent 84fc6a3 commit 74d8fe6

File tree

7 files changed

+25
-10
lines changed

7 files changed

+25
-10
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1313

1414
## [Unreleased]
1515

16+
TBD
17+
18+
## [1.2.0] - 2023-02-11
19+
20+
### Added
21+
22+
[#2](https://github.com/carlwiedemann/foresite/pull/2) Dynamic `<title>` tag based on post title
23+
1624
### Fixed
1725

1826
* Typo in README.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ Some facts:
7777

7878
`post.md.erb` is used to when running `foresite touch` for the default markdown content. It has two variables, `@title` for the post title and `@date_ymd` for the created date in ISO 8601 `YYYY-MM-DD` format. Modify to have different defaults when running `foresite touch`.
7979

80-
`wrapper.html.erb` wraps all of your markdown. Its sole variable `@content` will be a given post's HTML (converted from markdown). For the `index.html` file, `@content` will be an list of links to all posts in reverse-chronological order. Modify to have different overall page structure, or to add `<style>` etc.
80+
`wrapper.html.erb` wraps all of your markdown. It has two variables, `@title` for the post title that will populate the `<title>` tag, and `@content` for a given post's HTML (converted from markdown). For the `index.html` file, `@title` will be `nil`, and `@content` will be an list of links to all posts in reverse-chronological order. Modify to have different overall page structure, or to add `<style>` etc.
8181

8282
`_list.html.erb` is used to generate the `<ul>` list of posts on the `index.html` file. Modify to show posts in a different way.
8383

lib/foresite.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ def self.render_post(title, date_ymd)
8282
})
8383
end
8484

85-
def self.render_wrapped(markdown_content)
85+
def self.render_wrapped(title, markdown_content)
8686
render_erb_file(FILENAME_WRAPPER_HTML, {
87+
title: title,
8788
content: ::Kramdown::Document.new(markdown_content).to_html
8889
})
8990
end

lib/foresite/cli.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,12 @@ def build
9393

9494
links = markdown_paths.map do |markdown_path|
9595
markdown_content = File.read(markdown_path)
96+
title = markdown_content.split("\n").first { |line| /^# [a-z]/i =~ line }.gsub(/^#/, "").strip
9697

9798
filename_markdown = File.basename(markdown_path)
9899
html_path = Foresite.get_path_to_out_file(filename_markdown.gsub(/\.md$/, ".html"))
99100

100-
File.write(html_path, Foresite.render_wrapped(markdown_content))
101+
File.write(html_path, Foresite.render_wrapped(title, markdown_content))
101102
$stdout.puts("Created #{Foresite.relative_path(html_path)}")
102103

103104
# Extract date if it exists.
@@ -106,7 +107,7 @@ def build
106107
{
107108
date_ymd: match_data.nil? ? "" : match_data[0],
108109
href: Foresite.relative_path(html_path),
109-
title: markdown_content.split("\n").first { |line| /^# [a-z]/i =~ line }.gsub(/^#/, "").strip
110+
title: title
110111
}
111112
end
112113

lib/foresite/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Foresite
4-
VERSION = "1.1.3"
4+
VERSION = "1.2.0"
55
end

lib/skeleton/wrapper.html.erb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
<%
2+
index_title = 'Another Foresite Blog'
3+
-%>
14
<!DOCTYPE html>
25
<html lang="en">
36
<head>
47
<meta charset="utf-8">
5-
<title>Another Foresite Blog</title>
8+
<title><%= @title ? "#{@title} - #{index_title}" : index_title %></title>
69
<style></style>
710
</head>
811
<body>

spec/foresite/cli_spec.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,12 @@
204204
expect(File.read(expected_path_index)).to include(expected_content_index)
205205

206206
# They should also use the top-level HTML template, we can just use a dummy string to confirm.
207-
expected_template_content = "<title>Another Foresite Blog</title>"
208-
expect(File.read(expected_path_first)).to include(expected_template_content)
209-
expect(File.read(expected_path_second)).to include(expected_template_content)
210-
expect(File.read(expected_path_index)).to include(expected_template_content)
207+
expected_title_index = "<title>Another Foresite Blog</title>"
208+
expected_title_first = "<title>Jackdaws Love my Big Sphinx of Quartz! - Another Foresite Blog</title>"
209+
expected_title_second = "<title>When Zombies Arrive, Quickly Fax Judge Pat - Another Foresite Blog</title>"
210+
expect(File.read(expected_path_first)).to include(expected_title_first)
211+
expect(File.read(expected_path_second)).to include(expected_title_second)
212+
expect(File.read(expected_path_index)).to include(expected_title_index)
211213
end
212214
end
213215

0 commit comments

Comments
 (0)