index : paprika

A blogging platform written in Rust for Cloudflare Workers, integrated with Standard Notes

diff options
context:
space:
mode:
authorPeter Cai <[email protected]>2020-04-15 15:21:51 +0800
committerPeter Cai <[email protected]>2020-04-15 15:21:51 +0800
commite1033ff0b3c6a8eb64aa38d173f852f0e801260e (patch)
treecbcfd13f2e6170286bbf362743570e70b103f5e8
parent15f78ea6327a7beff12fc29e46ec9dac576adb27 (diff)
downloadpaprika-e1033ff0b3c6a8eb64aa38d173f852f0e801260e.tar.gz
support adding description meta tag to pages
using the page context
-rw-r--r--src/render.rs15
-rw-r--r--src/utils.rs8
-rw-r--r--theme/default/head.hbs1
3 files changed, 18 insertions, 6 deletions
diff --git a/src/render.rs b/src/render.rs
index 859c7bd..cb3a917 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -67,7 +67,11 @@ struct PageContext {
// The pathname part of the URL
pathname: String,
// The current query string
- query: String
+ query: String,
+ // Summary of the current page
+ // regardless of its type
+ // For use in <meta> tags
+ description: String
}
#[derive(Serialize)]
@@ -148,11 +152,12 @@ fn build_handlebars() -> Handlebars<'static> {
return hbs;
}
-fn build_page_context(url: &Url) -> PageContext {
+fn build_page_context(url: &Url, description: String) -> PageContext {
PageContext {
base_url: url.origin(),
pathname: url.pathname(),
- query: url.search()
+ query: url.search(),
+ description
}
}
@@ -168,7 +173,7 @@ async fn _render_homepage(url: Url, tpl_name: &str) -> MyResult<String> {
.map_err(|_| Error::BadRequest("Failed to parse query string".into()))?;
let mut context = HomePageContext {
blog: &ROOT_CONTEXT,
- page: build_page_context(&url),
+ page: build_page_context(&url, ROOT_CONTEXT.description.into()),
posts: vec![],
prev: None,
next: None
@@ -225,7 +230,7 @@ pub async fn render_post(url: Url, post: blog::Post) -> MyResult<String> {
let post_cache = blog::PostContentCache::find_or_render(&post).await;
let context = PostContext {
blog: &ROOT_CONTEXT,
- page: build_page_context(&url),
+ page: build_page_context(&url, strip_html_tags(&post_cache.summary)),
title: post.title,
url: post.url,
timestamp: post.timestamp,
diff --git a/src/utils.rs b/src/utils.rs
index 5b63db5..5552e3b 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -171,4 +171,10 @@ fn default_maxage() -> u64 {
60 * 60 * 24 * 7 // default to a week
}
-include!(concat!(env!("OUT_DIR"), "/build_timestamp.rs")); \ No newline at end of file
+include!(concat!(env!("OUT_DIR"), "/build_timestamp.rs"));
+
+// Strip HTML tags from a string via JS binding
+pub fn strip_html_tags(s: &str) -> String {
+ let js_str: JsString = s.into();
+ js_str.replace_by_pattern(&RegExp::new("(<([^>]+)>)", "ig"), "").into()
+} \ No newline at end of file
diff --git a/theme/default/head.hbs b/theme/default/head.hbs
index e2d5d0b..d45877c 100644
--- a/theme/default/head.hbs
+++ b/theme/default/head.hbs
@@ -1,6 +1,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
+ <meta name="description" content="{{ page.description }}">
<title>{{ #if title }}{{ title }} - {{ blog.title }}{{ else }}{{ blog.title }}{{ /if }}</title>
<link rel="stylesheet" href="/static/monokai-sublime.css?ver={{ build_num }}" />
<link rel="stylesheet" href="/static/style.css?ver={{ build_num }}" />