-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
118 lines (97 loc) · 4.75 KB
/
Program.cs
File metadata and controls
118 lines (97 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Generating example content for Blit.
using BlendInteractive.Blit;
using BlendInteractive.Blit.Xml;
using ExampleSite.Models.Pages;
var blogPostPage = Content
// Create or update a "ArticlePage" page type, using "blog-2024-01-05" as an identifier.
.Build<ArticlePage>("blog-2024-01-05", ContentActions.Create | ContentActions.Update)
// To see if a page already exists, query for a page that meets this criteria:
// * The `OldUrl` matches "/articles/blog-2024-01-05"
// * Is an `ArticlePage` content type
.Query(q => q
.Match(x => x.OldUrl, "/articles/blog-2024-01-05")
.OfType<ArticlePage>()
)
// If a page needs to be created, query for a parent page that meets this criteria:
// * An `ArticlesLandingPage` content type
// * Is an immediate child of a page with an ID matching the `homepage-id` variable
.ParentQuery(q => q
.OfType<ArticlesLandingPage>()
.Tree(TreeLocatorType.Child, t => t.Id(new VariableReference("homepage-id")))
)
// Set "stage-one" properties which are all the properties necessary for the
// page to exist, but none of the properties that may have dependencies on
// other content in the import.
.StageOneProperties(p =>
{
// Make sure to set OldUrl since that's part of how we're uniquely indentifying imported pages.
p.Text(x => x.OldUrl, "/articles/blog-2024-01-05");
// Name is always required
p.Text(x => x.Name, "Astronauts are just Star Sailors");
// Some other text can be set now.
p.Text(x => x.Title, "Astronauts are just Star Sailors");
p.Text(x => x.Blurb, "<p>Astron is star; nautes is sailor.</p>");
p.List(x => x.Category, cats =>
{
cats.Add(CategoryPathReference.Create("Sections", "News Article", "Best Articles"));
});
// Testing a few property types
p.Text(x => x.ExampleInteger, "-10");
p.Text(x => x.ExampleEnum, ExampleEnum.ValueTwo.ToString());
p.Text(x => x.ExampleUrlProperty, "https://www.blendinteractive.com/");
p.Text(x => x.ExampleReference, new VariableReference("homepage-id")); // Could also use a content query here.
// As an example, adding a reference to the homepage two different ways
p.List(x => x.ExampleContentReferenceList, refs =>
{
// As a content query
refs.Add(
ContentQuery.Build()
.Id(new VariableReference("homepage-id"))
.AsFragment(ContentEmbedType.ID, "")
);
// Or as a direct text value. As long as the text value parses to a valid
// page ID, this will work.
refs.Add(new VariableReference("homepage-id"));
});
})
.StageTwoProperties(p =>
{
// Stage Two is for properties that may reference other imported content.
// If there are no such properties, you can skip stage two with
// `.SkipStageTwo()` instead.
// Create HTML with an embedded link
p.Text(x => x.Body, new IFragment[] {
// Start of the HTML fragment.
new TextFragment("<p>Learn more about <a href=\""),
// Replace the link with a permanent link if it exists.
// Will search for an ancestor of the homepage-id value,
// with "/etymology-facts" as the OldUrl. If found, the
// internal permanent URL will be embedded here. If not,
// the fallback of "/etymology-facts" will be used.
new ContentLookupFragment(
ContentEmbedType.PermanentUrl,
new ContentQuery(ContentQuery.Build()
.Tree(TreeLocatorType.Ancestor, t => t.Id(new VariableReference("homepage-id")))
.Match("OldUrl", "/etymology-facts")
.Done()),
"/etymology-facts"
),
// Finish the link
new TextFragment("\">Etymology Facts!</a></p>")
});
});
IContentSerializer serializer = new XmlContentSerializer();
var xml = serializer.Serialize(blogPostPage);
// Each piece of content is written out as individual XML files.
File.WriteAllText("./blog-2024-01-05.xml", xml);
// Then an index file is written with references to each XML file,
// in the order they should be processed.
File.WriteAllText("./index.txt", "./blog-2024-01-05.xml");
// These local files are good for local testing, but for use on a webserver,
// they are typically moved to something like S3 or Azure Storage (or at least
// someplace where they can be accessible via HTTP or HTTPS). The index file
// is then rewritten to reference the files via their URLs. Then the index
// file URL can be queued for processing.
//
// Once queued, kick off the scheduled job!