1
1
// great primary https://404wolf.com/posts/blog/imageBlocks
2
2
import YamlParser from "js-yaml" ;
3
3
import {
4
- BLANK_BLOG_FRONTMATTER_FIELDS ,
5
- BlogFrontMatterFields , CACHE_NAME ,
4
+ BLANK_FRONTMATTER_FIELDS ,
5
+ FrontMatterFields , CACHE_NAME ,
6
6
DEFAULT_AUTHOR , STARTER_BLOG_FRONTMATTER_FIELDS ,
7
7
toMarkdownOptions
8
8
} from "../types/commontypes.ts" ;
@@ -25,9 +25,9 @@ import {showToast} from "../components/showToast.tsx";
25
25
import JSZip from "jszip" ;
26
26
27
27
28
- export const yamlToBlogFields = ( yamlstr : string ) : BlogFrontMatterFields => {
28
+ export const yamlToBlogFields = ( yamlstr : string ) : FrontMatterFields => {
29
29
if ( yamlstr . trim ( ) . length === 0 ) {
30
- return BLANK_BLOG_FRONTMATTER_FIELDS ;
30
+ return BLANK_FRONTMATTER_FIELDS ;
31
31
}
32
32
try {
33
33
// sanity check that the yamlstr doesn't include syntax that will throw an error
@@ -44,33 +44,39 @@ export const yamlToBlogFields = (yamlstr: string): BlogFrontMatterFields => {
44
44
const author = data ?. author ?? data ?. byline_str ?? "" ; // fallback logic for old format
45
45
46
46
// if we make every field a string, then this becomes easy.
47
- // Might be some reflection way to do this? Going to be verbose the first time .
47
+ // Might be some reflection way to do this? Going to be verbose for now .
48
48
// maybe json.parse could be used more?
49
- const result : BlogFrontMatterFields = {
50
- title : data . title ?? BLANK_BLOG_FRONTMATTER_FIELDS . title ,
51
- date : data . date ?? data . dateline_str ?? BLANK_BLOG_FRONTMATTER_FIELDS . date ,
52
- readtime_minutes : parseInt ( data . readtime_minutes ?? data . readtime_str ?? BLANK_BLOG_FRONTMATTER_FIELDS . date ) ,
53
- author : author . length ? author : BLANK_BLOG_FRONTMATTER_FIELDS . author ,
54
- permalink : data . permalink ?? BLANK_BLOG_FRONTMATTER_FIELDS . permalink ,
49
+ const result : FrontMatterFields = {
50
+ title : data . title ?? BLANK_FRONTMATTER_FIELDS . title ,
51
+ date : data . date ?? data . dateline_str ?? BLANK_FRONTMATTER_FIELDS . date ,
52
+ readtime_minutes : parseInt ( data . readtime_minutes ?? data . readtime_str ?? BLANK_FRONTMATTER_FIELDS . date ) ,
53
+ author : author . length ? author : BLANK_FRONTMATTER_FIELDS . author ,
54
+ permalink : data . permalink ?? BLANK_FRONTMATTER_FIELDS . permalink ,
55
55
tags : [ ] , // todo: fix
56
- basename : data . basename ?? BLANK_BLOG_FRONTMATTER_FIELDS . basename ,
56
+ basename : data . basename ?? BLANK_FRONTMATTER_FIELDS . basename ,
57
57
58
- carousel_title : data . carousel_title ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_title ,
59
- carousel_summary : data . carousel_summary ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_summary ,
60
- carousel_image : data . carousel_image ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_image ,
61
- carousel_image_alt_text : data . carousel_image_alt_text ?? BLANK_BLOG_FRONTMATTER_FIELDS . carousel_image_alt_text ,
62
- carousel_show : ( data . carousel_show === "true" ? "true" : BLANK_BLOG_FRONTMATTER_FIELDS . carousel_show ) ,
58
+ agency : data . agency ?? BLANK_FRONTMATTER_FIELDS . agency ,
59
+ project_url : data . project_url ?? BLANK_FRONTMATTER_FIELDS . project_url ,
60
+ impact_statement : [ ] , // todo: fix
61
+
62
+ carousel_title : data . carousel_title ?? BLANK_FRONTMATTER_FIELDS . carousel_title ,
63
+ carousel_summary : data . carousel_summary ?? BLANK_FRONTMATTER_FIELDS . carousel_summary ,
64
+ carousel_image : data . carousel_image ?? BLANK_FRONTMATTER_FIELDS . carousel_image ,
65
+ carousel_image_alt_text : data . carousel_image_alt_text ?? BLANK_FRONTMATTER_FIELDS . carousel_image_alt_text ,
66
+ carousel_show : ( data . carousel_show === "true" ? "true" : BLANK_FRONTMATTER_FIELDS . carousel_show ) ,
63
67
} ;
64
68
return result ;
65
69
} catch ( err ) {
66
70
console . error ( err ) ;
67
- return BLANK_BLOG_FRONTMATTER_FIELDS ;
71
+ return BLANK_FRONTMATTER_FIELDS ;
68
72
}
69
73
}
70
74
71
75
/** there's a pending change with the website to rename some fields */
72
- export const getYamlBlogHeaderNew = ( fields : BlogFrontMatterFields ) : string => {
73
- const tags = '[' + fields . tags ?. map ( s => `'${ s } '` ) . join ( ',' ) + ']' ;
76
+ export const getYamlBlogHeader = ( fields : FrontMatterFields ) : string => {
77
+ const tags = YamlParser . dump ( [ ...fields . tags ] ) ;
78
+ const impact_statement = YamlParser . dump ( fields . impact_statement ) ;
79
+
74
80
return `# Page template info (DO NOT EDIT)
75
81
layout: default
76
82
blog_page: true
@@ -92,11 +98,22 @@ readtime_minutes: "${fields.readtime_minutes}"
92
98
author: "${ fields . author } "
93
99
permalink: ${ fields . permalink }
94
100
basename: "${ fields . basename } "
95
- tags: [${ tags } ]
101
+ tags: ${ tags }
102
+
103
+ # These are in the process of being renamed and are here for the transition, they should be removed
104
+ dateline_str: "${ fields . date } "
105
+ byline_str: "${ fields . author } "
106
+
107
+ # Project details page specifics. (Edit this)
108
+ agency: ${ fields . agency }
109
+ project_url: ${ fields . project_url }
110
+
111
+ # Project Impact statement (Edit this)
112
+ impact_statement: ${ impact_statement }
96
113
` ;
97
114
}
98
115
99
- export const getYamlBlogHeader = ( fields : BlogFrontMatterFields ) : string => {
116
+ export const getYamlBlogHeaderOld = ( fields : FrontMatterFields ) : string => {
100
117
const tags = '[' + fields . tags ?. map ( s => `'${ s } '` ) . join ( ',' ) + ']' ;
101
118
const carousel_show = fields . carousel_show ? "true" : "false" ;
102
119
return `# Page template info (DO NOT EDIT)
@@ -144,7 +161,7 @@ export const generateBasename = (title: string): string => {
144
161
/**
145
162
* Just trying to consolidate all the url building to a single location.
146
163
*/
147
- export const generateFields = ( fields : BlogFrontMatterFields , resetPermalink = false ) : {
164
+ export const generateFields = ( fields : FrontMatterFields , resetPermalink = false ) : {
148
165
basename : string , // if this is undefined, then function failed
149
166
imagedir : string , // references the -img/ directory
150
167
carousel_imagepath_for_md : string , // starts with /news-and-blog, used in MD
@@ -174,7 +191,7 @@ export const generateFields = (fields: BlogFrontMatterFields, resetPermalink = f
174
191
return { basename, imagedir, carousel_imagepath_for_md, mdfilename, datedbasename, permalink} ;
175
192
} ;
176
193
177
- export const blogFieldsFixup = ( fields : BlogFrontMatterFields , resetPermalink = false ) : BlogFrontMatterFields => {
194
+ export const blogFieldsFixup = ( fields : FrontMatterFields , resetPermalink = false ) : FrontMatterFields => {
178
195
if ( fields . title . trim ( ) === "" ) {
179
196
showToast ( "Must have a title to continue." , "error" ) ;
180
197
return fields ;
@@ -223,7 +240,7 @@ function isParent(node: unknown): node is Mdast.Parent {
223
240
}
224
241
225
242
interface SaveDataType {
226
- yamlFields : BlogFrontMatterFields ;
243
+ yamlFields : FrontMatterFields ;
227
244
imagesFromMd : string [ ] ;
228
245
markdownFixedStr : string ; // fixes the cache image links to use zip save director
229
246
}
0 commit comments