~adnano/kiln-devel

This thread contains a patchset. You're looking at the original emails, but you may wish to use the patch review UI. Review patch
4 2

[PATCH kiln] page: extract title from first content line

Details
Message ID
<20231107091202.40839-1-git@olivier.pfad.fr>
DKIM signature
missing
Download raw message
Patch: +15 -0
when the title in the frontmatter title is empty

---

Hi,
I have been using kiln reliably for my blog for quite a while and really
enjoyed it.

I recently realized that I only use the frontmatter to specify the title
of my post (and that I was never quite sure of the syntax for it).
To simplify my workflow, I propose to add a fallback, which looks at the
first content line and set it as a title if it starts with "# " (common
title syntax for markdown/gmi files).

With this change, I can simply drop the frontmatter from all my blog
posts and simply start with the title (prefixed by "# ").

What do you think?
---
 page.go | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/page.go b/page.go
index 5679bbd..e302444 100644
--- a/page.go
+++ b/page.go
@@ -138,6 +138,21 @@ func (p *Page) _read(fsys fs.FS, path string, task *Task, cfg *Site) error {
				content = bytes.TrimLeft(content, "\r\n")
			}

			// If the frontmatter title is empty, try to extract it from the first content line
			// when it starts with "# " (title syntax for markdown and gemtext files)
			if page.Title == "" {
				if len(content) > 2 && content[0] == '#' && content[1] == ' ' {
					j := 2
					for ; j < len(content); j++ {
						if content[j] == '\r' || content[j] == '\n' {
							break
						}
					}
					page.Title = string(content[2:j])
					content = content[j:]
				}
			}

			if cmd, ok := task.preprocess[strings.TrimPrefix(ext, ".")]; ok {
				var buf bytes.Buffer
				if err := execute(cmd, bytes.NewReader(content), &buf); err != nil {
-- 
2.42.1
Details
Message ID
<CWSLR41B6MWW.36QYDHLTTS4PQ@maolood.com>
In-Reply-To
<20231107091202.40839-1-git@olivier.pfad.fr> (view parent)
DKIM signature
missing
Download raw message
> Hi,
> I have been using kiln reliably for my blog for quite a while and really
> enjoyed it.

Glad you like it!

> I recently realized that I only use the frontmatter to specify the title
> of my post (and that I was never quite sure of the syntax for it).
> To simplify my workflow, I propose to add a fallback, which looks at the
> first content line and set it as a title if it starts with "# " (common
> title syntax for markdown/gmi files).
>
> With this change, I can simply drop the frontmatter from all my blog
> posts and simply start with the title (prefixed by "# ").
>
> What do you think?

This is the way kiln used to work long ago. However, this was changed to
make kiln content-type agnostic, i.e. it makes no assumptions about the
format of the files you use besides the optional frontmatter. Also,
sometimes authors may want the first header to be part of the content
instead of the title of the page and might be surprised that the title
is automatically stripped. For these reasons I'm a bit hesitant to
change this back.
Details
Message ID
<72881511-fc8c-47fd-8b68-0d8e0861447a@app.fastmail.com>
In-Reply-To
<CWSLR41B6MWW.36QYDHLTTS4PQ@maolood.com> (view parent)
DKIM signature
missing
Download raw message
> sometimes authors may want the first header to be part of the content
> instead of the title of the page and might be surprised that the title
> is automatically stripped. For these reasons I'm a bit hesitant to
> change this back.

That's why I attempt the stripping only if the title in the frontmatter is empty.
This should only break for users who currently do not specify a title in the frontmatter (I would be surprised if anyone does that, since it's hard to display much about a page without a title).

If this is an issue, the user can still put a dummy title in the frontmatter:
  ---
  title: -
  ---
  # this title will not be stripped
Details
Message ID
<ba86fc77-e55c-42f8-8298-41ce910a0fd2@app.fastmail.com>
In-Reply-To
<72881511-fc8c-47fd-8b68-0d8e0861447a@app.fastmail.com> (view parent)
DKIM signature
missing
Download raw message
As alternative, one could trigger the fallback only if no frontmatter is present.

So “# some title” would be a frontmatter shorthand for
---
title: some title
---
Details
Message ID
<CWSYXIGXMQ23.3B3VSO2155A9J@maolood.com>
In-Reply-To
<ba86fc77-e55c-42f8-8298-41ce910a0fd2@app.fastmail.com> (view parent)
DKIM signature
missing
Download raw message
On Tue Nov 7, 2023 at 11:10 AM EST, Olivier C wrote:
> As alternative, one could trigger the fallback only if no frontmatter is present.
>
> So “# some title” would be a frontmatter shorthand for
> ---
> title: some title
> ---

I like this idea better. I'll give it some thought.
Reply to thread Export thread (mbox)