Before you yell at me how Hugo is a SSG, let me preface that I know that.
I am coming from Jekyll and my solution to a full site search is a small rest like php script to have a full text search with javascript on my site, without relying on external services.
So what I want to know is, how I can use the template system to generate a sql file in Hugo? In Jekyll I achieve this with this frontmatter:
---
layout: null
sitemap:
exclude: 'yes'
---
This makes sure no HTML is getting in the way and I strip the HTML from the markdown, getting pure text:
What you probably end up doing is to create a custom “SQL output format”, and then configure your site to publish the home page in both HTML and SQL. That way you can create a template in /layouts/index.sql (or something).
DROP TABLE IF EXISTS database.blog;
CREATE TABLE database..blog (
id INT NOT NULL AUTO_INCREMENT,
published DATE NOT NULL,
title TEXT NOT NULL,
body TEXT NOT NULL,
url varchar(100) NOT NULL,
image varchar(50) NOT NULL,
CONSTRAINT id_PK PRIMARY KEY (id)
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8mb4
COLLATE=utf8mb4_general_ci;
CREATE FULLTEXT INDEX blog_body_IDX ON database.blog (body);
{{ block "main" .}}
{{ end }}
home.sql.sql:
{{ define "main" }}
{{ range (where .Pages "Section" "ne" "gist") }}
{{ range .Pages }}
INSERT INTO database.blog (published, title, body, url, image)
VALUES(
'{{ .Date.Format "01.02.2006" }}',
'{{ plainify .Title }}',
'{{ (plainify .Content) }}',
'{{ .Permalink }}',
'{{ .Params.post_image }}'
);
{{ end }}
{{ end }}
{{ end }}
The range has a negative selection, since I do not need my gists to be searchable.
This is put into a mysql database and queried with js to create a site search.