Skip to content

Commit

Permalink
Output Statiq-friendly YAML frontmatter by default
Browse files Browse the repository at this point in the history
Add support for 'Updated' frontmatter
  • Loading branch information
abock committed Nov 15, 2021
1 parent 1fb2268 commit 35cbc85
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
42 changes: 28 additions & 14 deletions Goodbye.WordPress/MysqlPostReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ is string permalinkStructure &&
SELECT
p.id AS ID,
p.post_status AS Status,
p.post_date AS LocalDate,
p.post_date_gmt AS UtcDate,
p.post_date AS PublishedLocalDate,
p.post_date_gmt AS PublishedUtcDate,
p.post_modified AS UpdatedLocalDate,
p.post_modified_gmt AS UpdatedUtcDate,
p.post_name AS Name,
p.post_title AS Title,
c.name AS Category,
Expand Down Expand Up @@ -139,21 +141,30 @@ GROUP BY p.id

var postId = reader.GetInt32("ID");

var localDate = reader.GetDateTime("LocalDate");
var utcDate = reader.GetDateTime("UtcDate");
var date = localDate > DateTime.MinValue && utcDate> DateTime.MinValue
? new DateTimeOffset(localDate, localDate - utcDate)
: (DateTimeOffset?)null;
var publishedDate = ParseDate(
reader.GetDateTime("PublishedLocalDate"),
reader.GetDateTime("PublishedUtcDate"));

if (date is null)
var updatedDate = ParseDate(
reader.GetDateTime("UpdatedLocalDate"),
reader.GetDateTime("UpdatedUtcDate"));

if (publishedDate is null)
continue;

var postName = reader.GetString("Name");

var originalUrl = ExpandPermalinkStructure(
originalPermalinkStructure,
publishedDate.Value,
postId,
postName);

yield return new Post(
postId,
reader.GetString("Status"),
date,
publishedDate,
updatedDate,
postName,
reader.GetString("Title"),
reader.GetString("Category") is string category &&
Expand All @@ -169,15 +180,18 @@ GROUP BY p.id
.Distinct()
.ToImmutableList(),
reader.GetString("Content"),
ExpandPermalinkStructure(
originalPermalinkStructure,
date.Value,
postId,
postName),
originalUrl is null
? ImmutableList<string>.Empty
: ImmutableList.Create(originalUrl),
ImmutableList<PostResource>.Empty);
}
}

static DateTimeOffset? ParseDate(DateTime localDate, DateTime utcDate)
=> localDate > DateTime.MinValue && utcDate > DateTime.MinValue
? new DateTimeOffset(localDate, localDate - utcDate)
: null;

static string? ExpandPermalinkStructure(
string? originalPermalinkStructure,
DateTimeOffset date,
Expand Down
5 changes: 3 additions & 2 deletions Goodbye.WordPress/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ namespace Goodbye.WordPress
public sealed record Post(
int Id,
string? Status,
DateTimeOffset? Date,
DateTimeOffset? Published,
DateTimeOffset? Updated,
string Name,
string Title,
string? Category,
ImmutableList<string> Tags,
string Content,
string? OriginalUrl,
ImmutableList<string> RedirectFrom,
ImmutableList<PostResource> Resources);
}
2 changes: 1 addition & 1 deletion Goodbye.WordPress/WordPressExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public async Task<WordPressExporter> ExportAsync(

Log.Information("Post {Title} ({Date}) → {Path}",
processedPost.Title,
processedPost.Date,
processedPost.Published,
Path.GetRelativePath(
Environment.CurrentDirectory,
outputPath));
Expand Down
26 changes: 16 additions & 10 deletions Goodbye.WordPress/WordPressExporterDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public virtual string GetOutputPath(
Post post)
=> Path.Combine(
exporter.ContentOutputDirectory,
$"{post.Date:yyyy-MM-dd}-{post.Name}");
$"{post.Published:yyyy-MM-dd}-{post.Name}");

public virtual StreamWriter GetStreamWriter(
WordPressExporter exporter,
Expand Down Expand Up @@ -230,15 +230,19 @@ public virtual void PopulatePostYamlFrontMatter(
Post post,
YamlMappingNode rootNode)
{
void AddDateField(string name, DateTimeOffset? date)
{
if (date.HasValue)
rootNode.Add(name, date.Value.ToString(
"yyyy-MM-dd HH:mm:ss zzz",
CultureInfo.InvariantCulture));
}

if (!string.IsNullOrEmpty(post.Title))
rootNode.Add(nameof(Post.Title), post.Title);

if (post.Date.HasValue)
rootNode.Add(
nameof(Post.Date),
post.Date.Value.ToString(
"yyyy-MM-dd HH:mm:ss zzz",
CultureInfo.InvariantCulture));
AddDateField(nameof(Post.Published), post.Published);
AddDateField(nameof(Post.Updated), post.Updated);

if (!string.IsNullOrEmpty(post.Category))
rootNode.Add(
Expand All @@ -256,10 +260,12 @@ public virtual void PopulatePostYamlFrontMatter(
nameof(Post.Status),
post.Status);

if (!string.IsNullOrEmpty(post.OriginalUrl))
if (post.RedirectFrom.Count > 0)
// https://statiq.dev/web/content-and-data/content/redirects
rootNode.Add(
nameof(Post.OriginalUrl),
post.OriginalUrl);
nameof(Post.RedirectFrom),
new YamlSequenceNode(
post.RedirectFrom.Select(r => new YamlScalarNode(r))));
}

public virtual IEnumerable<Uri> GetDownloadResourceUris(
Expand Down

0 comments on commit 35cbc85

Please sign in to comment.