@taprootio/site-authoring 0.42.0 → 0.56.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +110 -12
- package/examples/riverbend-wellness/settings/site-publishing-preferences.json +3 -0
- package/examples/riverbend-wellness/settings/taproot-styles.json +11 -0
- package/package.json +4 -3
- package/src/api.js +242 -23
- package/src/appearance-contract.js +264 -3
- package/src/authored-tag-names.js +88 -0
- package/src/authors-contract.js +71 -3
- package/src/block-flow.js +103 -1
- package/src/brand-color.js +22 -0
- package/src/breadcrumb-artwork.js +234 -0
- package/src/breadcrumb-separators.js +11 -0
- package/src/changelog.json +160 -0
- package/src/chrome-features.js +6 -2
- package/src/chrome-input.js +1 -0
- package/src/chrome-paint-catalog-taproot.json +3 -3
- package/src/chrome-report.js +91 -12
- package/src/cli.js +136 -38
- package/src/constants.js +1 -0
- package/src/content/components.js +27 -4
- package/src/content/external-link.js +2 -1
- package/src/content/markdown.js +271 -101
- package/src/content/tiptap-prosemirror.ts +125 -43
- package/src/content/validate-document.js +4 -1
- package/src/content-inset.js +48 -0
- package/src/errors.js +1 -0
- package/src/espalier-shared.js +26 -0
- package/src/field-suggestions.js +40 -0
- package/src/fixture-init.js +22 -5
- package/src/font-catalog.js +11 -1
- package/src/footer-brand-inputs.js +22 -3
- package/src/footer-content.js +1 -0
- package/src/footer-contract.js +11 -0
- package/src/footer-cta-styles.js +6 -0
- package/src/footer-draft-hash.js +4 -2
- package/src/footer-fit.js +62 -15
- package/src/footer-workspace.js +44 -7
- package/src/latest-posts-tags.js +57 -0
- package/src/page-metadata.js +15 -13
- package/src/page-structure-checks.js +5 -0
- package/src/page-tag-badge-modes.js +6 -0
- package/src/page-tag-writes.js +158 -0
- package/src/page-type-tags.js +16 -0
- package/src/presentation-reference.js +297 -86
- package/src/recipe-page-options.js +22 -0
- package/src/recipe-step-contract.js +164 -0
- package/src/recipe-step-contract.json +65 -0
- package/src/reference-help.js +182 -79
- package/src/session.js +39 -24
- package/src/setting-reference.js +239 -48
- package/src/settings-catalog.js +63 -0
- package/src/site-check-findings.js +23 -5
- package/src/site-check-rules.json +1 -0
- package/src/site-health-findings.js +13 -13
- package/src/styling-fixed-values.js +71 -0
- package/src/tag-destination-warnings.js +35 -0
- package/src/template-token-reach.json +242 -0
- package/src/theme-proposals.js +18 -27
- package/src/theme-resolve.js +265 -23
- package/src/theme-validation.js +118 -7
- package/src/token-reach.js +222 -0
- package/src/typed-page-reference.js +94 -16
- package/src/typed-pages.js +233 -74
- package/src/verbs/authors.js +198 -33
- package/src/verbs/deploy.js +6 -0
- package/src/verbs/footer-push.js +30 -5
- package/src/verbs/forms-push.js +11 -3
- package/src/verbs/index.js +4 -2
- package/src/verbs/pages-delete.js +19 -10
- package/src/verbs/pages-push.js +225 -71
- package/src/verbs/plan.js +10 -4
- package/src/verbs/pull.js +324 -69
- package/src/verbs/staging-review.js +27 -1
- package/src/verbs/status.js +22 -0
- package/src/verbs/theme-push.js +93 -25
- package/src/verbs/theme-resolve.js +189 -55
- package/src/verbs/validate.js +6 -5
- package/src/workspace-check.js +35 -3
- package/src/workspace.js +73 -2
- package/test/fixtures/default-site-theme.json +516 -0
package/src/typed-pages.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { FREE_FORM_TEMPLATE_VERSION, TEMPLATE_TYPE_FREE_FORM } from "./api.js";
|
|
2
|
+
import { normalizeAuthorHandle } from "./authors-contract.js";
|
|
3
|
+
import { authoredTagSlug, validateAuthoredTagNames } from "./authored-tag-names.js";
|
|
2
4
|
import { isVideoEmbedId } from "./content/video-embed-url.js";
|
|
3
|
-
import { SiteAuthoringError } from "./errors.js";
|
|
5
|
+
import { hasControlCharacter, SiteAuthoringError } from "./errors.js";
|
|
4
6
|
import { isImageDescriptionMode } from "./content/image-description-validation.js";
|
|
5
7
|
import { stripImageDescriptionHydration } from "./content/image-description-document.js";
|
|
6
8
|
import { requireDescriptionLength } from "./page-metadata.js";
|
|
9
|
+
import { recipeStepProblems } from "./recipe-step-contract.js";
|
|
7
10
|
|
|
8
11
|
/**
|
|
9
12
|
* The authored page templates (TR00893).
|
|
@@ -13,7 +16,7 @@ import { requireDescriptionLength } from "./page-metadata.js";
|
|
|
13
16
|
* Free-form keeps its historical shape (the ProseMirror document itself). The
|
|
14
17
|
* other templates use a small wrapper:
|
|
15
18
|
*
|
|
16
|
-
* { "template": "recipe", "displayDate": "2024-05-01", "coverImageId": "…", "data": { … } }
|
|
19
|
+
* { "template": "recipe", "displayDate": "2024-05-01", "displayAuthor": "jane-doe", "showOnProfile": false, "coverImageId": "…", "data": { … } }
|
|
17
20
|
*
|
|
18
21
|
* `pull` writes that wrapper, a Markdown source is converted into it, and
|
|
19
22
|
* `pages push` turns it into the API's `PageTemplate`. Because all three meet
|
|
@@ -61,7 +64,7 @@ export const SHARED_FRONT_MATTER_KEYS = Object.freeze(["title", "path", "descrip
|
|
|
61
64
|
* its bare document, which has nowhere to carry them, so it does not accept
|
|
62
65
|
* them.
|
|
63
66
|
*/
|
|
64
|
-
const TYPED_PAGE_FRONT_MATTER_KEYS = Object.freeze(["displayDate", "coverImage"]);
|
|
67
|
+
const TYPED_PAGE_FRONT_MATTER_KEYS = Object.freeze(["displayDate", "displayAuthor", "showOnProfile", "coverImage", "tags"]);
|
|
65
68
|
|
|
66
69
|
/** Front-matter fields only one template accepts. Others are refused for it. */
|
|
67
70
|
const TEMPLATE_FRONT_MATTER_KEYS = Object.freeze({
|
|
@@ -171,6 +174,36 @@ export function isAuthorableTemplateType(wireType) {
|
|
|
171
174
|
return templateNameForWireType(wireType) !== undefined;
|
|
172
175
|
}
|
|
173
176
|
|
|
177
|
+
/** Whether the workspace template accepts an authored tag list. */
|
|
178
|
+
export function supportsAuthoredTags(template) {
|
|
179
|
+
return Object.hasOwn(TEMPLATE_FRONT_MATTER_KEYS, template) && TEMPLATE_FRONT_MATTER_KEYS[template].includes("tags");
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** Whether a site's wire template supports authored tags. */
|
|
183
|
+
export function supportsAuthoredTagWireType(wireType) {
|
|
184
|
+
return supportsAuthoredTags(templateNameForWireType(wireType));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** A snapshot can have valid wire data that a present source list could not write back. */
|
|
188
|
+
export function projectAuthoredTagSnapshot({ entries }) {
|
|
189
|
+
const projected = validateAuthoredTagNames(entries.map(({ name }) => name));
|
|
190
|
+
if ("reason" in projected) return projected;
|
|
191
|
+
|
|
192
|
+
const bySlug = new Map();
|
|
193
|
+
for (const [index, entry] of entries.entries()) {
|
|
194
|
+
const name = projected.names[index];
|
|
195
|
+
const slug = authoredTagSlug(name);
|
|
196
|
+
if (hasControlCharacter(entry.name)) {
|
|
197
|
+
return { field: `tags[${index}]`, reason: "A stored tag name contains control characters." };
|
|
198
|
+
}
|
|
199
|
+
if (slug !== entry.slug.toLowerCase()) {
|
|
200
|
+
return { field: `tags[${index}]`, reason: "A stored tag name cannot round-trip to its current slug." };
|
|
201
|
+
}
|
|
202
|
+
bySlug.set(slug, name);
|
|
203
|
+
}
|
|
204
|
+
return { names: projected.slugs.map((slug) => bySlug.get(slug)), slugs: projected.slugs };
|
|
205
|
+
}
|
|
206
|
+
|
|
174
207
|
/** Whether this wire template type is a legal document. */
|
|
175
208
|
export function isLegalTemplateType(wireType) {
|
|
176
209
|
return wireType === PAGE_TEMPLATES[TEMPLATE_LEGAL].wireType;
|
|
@@ -273,6 +306,43 @@ function requireDocument(value, file, path, { nonEmpty = false } = {}) {
|
|
|
273
306
|
return value;
|
|
274
307
|
}
|
|
275
308
|
|
|
309
|
+
export const RECIPE_STEP_UNSUPPORTED = "pages.recipe_step_unsupported";
|
|
310
|
+
|
|
311
|
+
function appendRecipeStepProblems(problems, issues, { file, name, sectionIndex, stepIndex, firstLine }) {
|
|
312
|
+
const section = name?.trim() ? `section ${sectionIndex + 1} ('${name.trim()}')` : `section ${sectionIndex + 1}`;
|
|
313
|
+
const stepPath = `data.instructionSections[${sectionIndex}].stepBodies[${stepIndex}]`;
|
|
314
|
+
for (const issue of issues) {
|
|
315
|
+
const line = issue.line ?? firstLine;
|
|
316
|
+
const source = line === undefined ? "" : ` (line ${line})`;
|
|
317
|
+
problems.push({
|
|
318
|
+
code: RECIPE_STEP_UNSUPPORTED,
|
|
319
|
+
file,
|
|
320
|
+
field: stepPath + (issue.path ?? ""),
|
|
321
|
+
...(line === undefined ? {} : { line }),
|
|
322
|
+
message: `'${file}', ${section}, step ${stepIndex + 1}${source}. ${issue.message}`,
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function refuseRecipeStepProblems(problems) {
|
|
328
|
+
const first = problems[0];
|
|
329
|
+
if (first === undefined) return;
|
|
330
|
+
throw fail(first.code, first.message, first.field).withProblems(problems);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
export function requireRecipeStepDocuments(sections, file) {
|
|
334
|
+
const problems = [];
|
|
335
|
+
for (const [sectionIndex, { name, stepBodies }] of sections.entries()) {
|
|
336
|
+
for (const [stepIndex, step] of stepBodies.entries()) {
|
|
337
|
+
appendRecipeStepProblems(problems, recipeStepProblems(step), { file, name, sectionIndex, stepIndex });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
refuseRecipeStepProblems(problems);
|
|
341
|
+
if (!sections.some(({ stepBodies }) => stepBodies.length > 0)) {
|
|
342
|
+
throw fail("pages.recipe_incomplete", `'${file}' needs at least one instruction step with text.`, file);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
276
346
|
function requireArray(value, file, path) {
|
|
277
347
|
if (!Array.isArray(value)) throw fail("pages.document_shape", `'${file}' ${path} must be a list.`, file);
|
|
278
348
|
return value;
|
|
@@ -310,6 +380,43 @@ export function requireDisplayDate(value, file) {
|
|
|
310
380
|
return value;
|
|
311
381
|
}
|
|
312
382
|
|
|
383
|
+
/**
|
|
384
|
+
* A page's display author (TR01402): a site author's handle, folded, or ""
|
|
385
|
+
* to clear it. Every handle names an author, including `none`; a member's
|
|
386
|
+
* email address is not one, because only a site author can be a byline.
|
|
387
|
+
*/
|
|
388
|
+
export function requireDisplayAuthor(value, file) {
|
|
389
|
+
if (value === "") return value;
|
|
390
|
+
const handle = normalizeAuthorHandle(value);
|
|
391
|
+
if (handle === undefined) {
|
|
392
|
+
throw fail(
|
|
393
|
+
"pages.display_author_invalid",
|
|
394
|
+
`'${file}' declares displayAuthor '${typeof value === "string" ? value.slice(0, 80) : ""}', which is not a `
|
|
395
|
+
+ "site author's handle (lowercase letters, digits and single hyphens, such as jane-doe). Use '' to clear it.",
|
|
396
|
+
file,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
return handle;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Whether the page may appear on its author's Taproot profile (TR01403): the
|
|
404
|
+
* literal `true` or `false`. A `.pm.json` document holds a JSON boolean; a
|
|
405
|
+
* Markdown front-matter line holds the text (`fromText`), which an unquoted or
|
|
406
|
+
* quoted literal both give. Anything else, a JSON string included, is refused
|
|
407
|
+
* so a typo never leaves a page on a profile its author meant to keep it off.
|
|
408
|
+
*/
|
|
409
|
+
export function requireShowOnProfile(value, file, { fromText = false } = {}) {
|
|
410
|
+
if (typeof value === "boolean") return value;
|
|
411
|
+
if (fromText && (value === "true" || value === "false")) return value === "true";
|
|
412
|
+
throw fail(
|
|
413
|
+
"pages.show_on_profile_invalid",
|
|
414
|
+
`'${file}' declares showOnProfile ${typeof value === "string" ? `'${value.slice(0, 80)}'` : "with a value"}, which is `
|
|
415
|
+
+ `not ${fromText ? "true or false" : "a JSON boolean (true or false)"}.`,
|
|
416
|
+
file,
|
|
417
|
+
);
|
|
418
|
+
}
|
|
419
|
+
|
|
313
420
|
// ── Canonical data per template ──────────────────────────────────────────────
|
|
314
421
|
|
|
315
422
|
const INGREDIENT_KEYS = ["rawText", "quantity", "unit", "name", "parsed"];
|
|
@@ -539,18 +646,12 @@ function canonicalSourceData(template, data, file) {
|
|
|
539
646
|
const at = `${path}.instructionSections[${sectionIndex}]`;
|
|
540
647
|
requireKeys(section, ["name", "stepBodies"], file, at);
|
|
541
648
|
if (section.name !== undefined) requireString(section.name, file, `${at}.name`);
|
|
542
|
-
requireArray(section.stepBodies, file, `${at}.stepBodies`)
|
|
543
|
-
requireDocument(step, file, `${at}.stepBodies[${index}]`, { nonEmpty: true })
|
|
544
|
-
);
|
|
649
|
+
requireArray(section.stepBodies, file, `${at}.stepBodies`);
|
|
545
650
|
});
|
|
651
|
+
requireRecipeStepDocuments(instructionSections, file);
|
|
546
652
|
const hasIngredient = ingredientGroups.some((group) => group.ingredients.length > 0);
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
throw fail(
|
|
550
|
-
"pages.recipe_incomplete",
|
|
551
|
-
`'${file}' is a recipe, which needs at least one ingredient and one instruction step.`,
|
|
552
|
-
file,
|
|
553
|
-
);
|
|
653
|
+
if (!hasIngredient) {
|
|
654
|
+
throw fail("pages.recipe_incomplete", `'${file}' needs at least one ingredient.`, file);
|
|
554
655
|
}
|
|
555
656
|
if (data.prepTimeMinutes !== undefined) {
|
|
556
657
|
requireCount(data.prepTimeMinutes, file, `${path}.prepTimeMinutes`, MAXIMUM_MINUTES);
|
|
@@ -572,19 +673,23 @@ function canonicalSourceData(template, data, file) {
|
|
|
572
673
|
* The canonical workspace document for a typed page, with keys in one fixed
|
|
573
674
|
* order. The order is part of the byte-stability contract.
|
|
574
675
|
*/
|
|
575
|
-
function typedDocument(template, { displayDate, coverImageId, data }) {
|
|
676
|
+
function typedDocument(template, { displayDate, displayAuthor, showOnProfile, coverImageId, tags, data }) {
|
|
576
677
|
return {
|
|
577
678
|
template,
|
|
578
679
|
...(displayDate === undefined ? {} : { displayDate }),
|
|
680
|
+
...(displayAuthor === undefined ? {} : { displayAuthor }),
|
|
681
|
+
...(showOnProfile === undefined ? {} : { showOnProfile }),
|
|
579
682
|
...(coverImageId === undefined ? {} : { coverImageId }),
|
|
683
|
+
...(tags === undefined ? {} : { tags }),
|
|
580
684
|
data,
|
|
581
685
|
};
|
|
582
686
|
}
|
|
583
687
|
|
|
584
688
|
/** Validates a parsed `.pm.json` that declares a template, and returns its canonical form. */
|
|
585
689
|
export function typedDocumentFromJson(parsed, file) {
|
|
586
|
-
requireKeys(parsed, ["template", "displayDate", "coverImageId", "data"], file, "document");
|
|
690
|
+
requireKeys(parsed, ["template", "displayDate", "displayAuthor", "showOnProfile", "coverImageId", "tags", "data"], file, "document");
|
|
587
691
|
const template = requireTemplateName(parsed.template, file);
|
|
692
|
+
const tags = Object.hasOwn(parsed, "tags") ? requireSourceTags(template, parsed.tags, file).names : undefined;
|
|
588
693
|
if (template === TEMPLATE_FREE_FORM) {
|
|
589
694
|
throw fail(
|
|
590
695
|
"pages.document_shape",
|
|
@@ -593,26 +698,40 @@ export function typedDocumentFromJson(parsed, file) {
|
|
|
593
698
|
file,
|
|
594
699
|
);
|
|
595
700
|
}
|
|
596
|
-
if ((template === TEMPLATE_GENERATED || template === TEMPLATE_LEGAL) && (parsed.displayDate !== undefined || parsed.coverImageId !== undefined)) {
|
|
701
|
+
if ((template === TEMPLATE_GENERATED || template === TEMPLATE_LEGAL) && (parsed.displayDate !== undefined || parsed.displayAuthor !== undefined || parsed.showOnProfile !== undefined || parsed.coverImageId !== undefined)) {
|
|
597
702
|
throw fail(
|
|
598
703
|
"pages.document_shape",
|
|
599
|
-
`'${file}' is a ${template} page, which has no displayDate or coverImageId of its own: its document is `
|
|
704
|
+
`'${file}' is a ${template} page, which has no displayDate, displayAuthor, showOnProfile or coverImageId of its own: its document is `
|
|
600
705
|
+ "{ template, data }.",
|
|
601
706
|
file,
|
|
602
707
|
);
|
|
603
708
|
}
|
|
604
709
|
const displayDate = parsed.displayDate === undefined ? undefined : requireDisplayDate(parsed.displayDate, file);
|
|
710
|
+
const displayAuthor = parsed.displayAuthor === undefined ? undefined : requireDisplayAuthor(parsed.displayAuthor, file);
|
|
711
|
+
const showOnProfile = parsed.showOnProfile === undefined ? undefined : requireShowOnProfile(parsed.showOnProfile, file);
|
|
605
712
|
let coverImageId;
|
|
606
713
|
if (parsed.coverImageId !== undefined) {
|
|
607
714
|
coverImageId = parsed.coverImageId === "" ? "" : requireIdentifier(parsed.coverImageId, file, "coverImageId");
|
|
608
715
|
}
|
|
609
716
|
return typedDocument(template, {
|
|
610
717
|
displayDate,
|
|
718
|
+
displayAuthor,
|
|
719
|
+
showOnProfile,
|
|
611
720
|
coverImageId,
|
|
721
|
+
tags,
|
|
612
722
|
data: canonicalSourceData(template, parsed.data, file),
|
|
613
723
|
});
|
|
614
724
|
}
|
|
615
725
|
|
|
726
|
+
function requireSourceTags(template, value, file) {
|
|
727
|
+
if (!supportsAuthoredTags(template)) {
|
|
728
|
+
throw fail("pages.tags_unsupported", `'${file}' declares tags, which the ${template} template does not accept.`, "tags");
|
|
729
|
+
}
|
|
730
|
+
const result = validateAuthoredTagNames(value);
|
|
731
|
+
if ("reason" in result) throw fail("pages.tags_invalid", `'${file}', ${result.field}. ${result.reason}`, result.field);
|
|
732
|
+
return result;
|
|
733
|
+
}
|
|
734
|
+
|
|
616
735
|
// ── The site's page → the workspace document (pull) ──────────────────────────
|
|
617
736
|
|
|
618
737
|
const ratingName = (wire) => RATING_NAME_BY_WIRE[wire];
|
|
@@ -709,6 +828,11 @@ export function workspaceDocumentFromPage(page, listedTemplateType) {
|
|
|
709
828
|
displayDate: !generated && typeof page.displayDate === "string" && page.displayDate !== ""
|
|
710
829
|
? page.displayDate
|
|
711
830
|
: undefined,
|
|
831
|
+
displayAuthor: !generated && typeof page.displayAuthor === "string" && page.displayAuthor !== ""
|
|
832
|
+
? normalizeAuthorHandle(page.displayAuthor)
|
|
833
|
+
: undefined,
|
|
834
|
+
// Written only when the page is kept off its author's profile, as the date is written only when there is one.
|
|
835
|
+
showOnProfile: !generated && page.showOnProfile === false ? false : undefined,
|
|
712
836
|
coverImageId: !generated && typeof page.coverImageId === "string" && page.coverImageId !== ""
|
|
713
837
|
? page.coverImageId
|
|
714
838
|
: undefined,
|
|
@@ -729,12 +853,12 @@ export function contentDocuments(document_) {
|
|
|
729
853
|
};
|
|
730
854
|
add(data.body, "data.body");
|
|
731
855
|
// The server accepts an empty introduction (the editor saves one for every
|
|
732
|
-
// recipe and album that has none), so it is validated but may be blank.
|
|
733
|
-
//
|
|
856
|
+
// recipe and album that has none), so it is validated but may be blank.
|
|
857
|
+
// Introductions accept fewer nodes than free-form and more than recipe steps.
|
|
734
858
|
add(data.introductionBody, "data.introductionBody", { mayBeEmpty: true, restricted: true });
|
|
735
859
|
for (const [sectionIndex, section] of (data.instructionSections ?? []).entries()) {
|
|
736
860
|
for (const [stepIndex, step] of section.stepBodies.entries()) {
|
|
737
|
-
add(step, `data.instructionSections[${sectionIndex}].stepBodies[${stepIndex}]`, {
|
|
861
|
+
add(step, `data.instructionSections[${sectionIndex}].stepBodies[${stepIndex}]`, { recipeStep: true });
|
|
738
862
|
}
|
|
739
863
|
}
|
|
740
864
|
return found;
|
|
@@ -746,8 +870,12 @@ export function sentDisplayDate(wireType) {
|
|
|
746
870
|
return name !== undefined && name !== TEMPLATE_FREE_FORM && name !== TEMPLATE_GENERATED && name !== TEMPLATE_LEGAL;
|
|
747
871
|
}
|
|
748
872
|
|
|
749
|
-
|
|
750
|
-
|
|
873
|
+
/** `displayAuthor` and `showOnProfile` travel with the same four byline templates as `displayDate` (TR01402, TR01403). */
|
|
874
|
+
export const sentDisplayAuthor = sentDisplayDate;
|
|
875
|
+
export const sentShowOnProfile = sentDisplayDate;
|
|
876
|
+
|
|
877
|
+
// The nodes the API accepts in an album or recipe introduction
|
|
878
|
+
// (`MembershipTermsDocumentRules.AllowedNodeTypes`). A page body accepts
|
|
751
879
|
// more, such as `section` and `inlineFacts`, so a document valid as a body can
|
|
752
880
|
// still be refused there, after earlier pages have been saved.
|
|
753
881
|
const RESTRICTED_NODE_TYPES = new Set([
|
|
@@ -768,8 +896,8 @@ const RESTRICTED_NODE_TYPES = new Set([
|
|
|
768
896
|
]);
|
|
769
897
|
|
|
770
898
|
/**
|
|
771
|
-
* The first node type
|
|
772
|
-
*
|
|
899
|
+
* The first node type a restricted introduction cannot hold, or
|
|
900
|
+
* `undefined`. A table is allowed only directly
|
|
773
901
|
* under the document root; the content validator already checks its cells.
|
|
774
902
|
*/
|
|
775
903
|
export function unsupportedRestrictedNode(doc) {
|
|
@@ -944,6 +1072,8 @@ export function wirePageFields(document_) {
|
|
|
944
1072
|
if ([TEMPLATE_FREE_FORM, TEMPLATE_LEGAL].includes(documentTemplate(document_))) return {};
|
|
945
1073
|
return definedEntries([
|
|
946
1074
|
["displayDate", document_.displayDate],
|
|
1075
|
+
["displayAuthor", document_.displayAuthor],
|
|
1076
|
+
["showOnProfile", document_.showOnProfile],
|
|
947
1077
|
["coverImageId", document_.coverImageId],
|
|
948
1078
|
]);
|
|
949
1079
|
}
|
|
@@ -959,12 +1089,12 @@ const IMAGE_LINE = /^!\[([^\]]*)\]\(\s*([^)\s]+)(?:\s+"[^"]*")?\s*\)$/u;
|
|
|
959
1089
|
const CONTINUATION = /^(?: {2,}|\t)/u;
|
|
960
1090
|
|
|
961
1091
|
/** Splits a body at its level-2 headings, ignoring anything inside a code fence. */
|
|
962
|
-
function splitSections(markdown) {
|
|
1092
|
+
function splitSections(markdown, firstLine = 1) {
|
|
963
1093
|
const lead = [];
|
|
964
1094
|
const sections = [];
|
|
965
1095
|
let current;
|
|
966
1096
|
let fence;
|
|
967
|
-
for (const line of markdown.split(/\r?\n/u)) {
|
|
1097
|
+
for (const [index, line] of markdown.split(/\r?\n/u).entries()) {
|
|
968
1098
|
const fenceMatch = FENCE.exec(line);
|
|
969
1099
|
if (fenceMatch !== null) {
|
|
970
1100
|
if (fence === undefined) fence = fenceMatch[1];
|
|
@@ -972,7 +1102,7 @@ function splitSections(markdown) {
|
|
|
972
1102
|
}
|
|
973
1103
|
const heading = fence === undefined && fenceMatch === null ? H2.exec(line) : null;
|
|
974
1104
|
if (heading !== null) {
|
|
975
|
-
current = { title: heading[1].trim(), lines: [] };
|
|
1105
|
+
current = { title: heading[1].trim(), lines: [], firstLine: firstLine + index + 1 };
|
|
976
1106
|
sections.push(current);
|
|
977
1107
|
} else {
|
|
978
1108
|
(current?.lines ?? lead).push(line);
|
|
@@ -1011,86 +1141,94 @@ function dedent(line, width) {
|
|
|
1011
1141
|
return line.slice(removed);
|
|
1012
1142
|
}
|
|
1013
1143
|
|
|
1014
|
-
function groupedLines(lines,
|
|
1144
|
+
function groupedLines({ lines, firstLine }, file, itemPattern, describe) {
|
|
1015
1145
|
const groups = [{ name: "", items: [] }];
|
|
1016
|
-
let
|
|
1017
|
-
// How far the open item's marker pushes its text in ("1. " is 3), which is
|
|
1018
|
-
// how far its continuation lines are indented. Removing exactly that much
|
|
1019
|
-
// keeps the relative indentation of nested lists and code inside the step.
|
|
1146
|
+
let item;
|
|
1020
1147
|
let markerWidth = 0;
|
|
1021
1148
|
let blanks = 0;
|
|
1022
|
-
const closeStep = () => {
|
|
1023
|
-
if (step !== undefined) groups.at(-1).items.push(step.join("\n"));
|
|
1024
|
-
step = undefined;
|
|
1025
|
-
};
|
|
1026
1149
|
let fence;
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1150
|
+
const closeItem = () => {
|
|
1151
|
+
if (item !== undefined) groups.at(-1).items.push({ text: item.lines.join("\n"), line: item.line });
|
|
1152
|
+
item = undefined;
|
|
1153
|
+
};
|
|
1154
|
+
const readFence = (text) => {
|
|
1155
|
+
const delimiter = /^( {0,3})(```+|~~~+)/u.exec(text)?.[2];
|
|
1156
|
+
if (fence === undefined) fence = delimiter;
|
|
1157
|
+
else if (delimiter?.[0] === fence[0] && delimiter.length >= fence.length && text.trim() === delimiter) fence = undefined;
|
|
1158
|
+
};
|
|
1159
|
+
for (const [index, line] of lines.entries()) {
|
|
1032
1160
|
if (line.trim() === "") {
|
|
1033
1161
|
blanks += 1;
|
|
1034
1162
|
continue;
|
|
1035
1163
|
}
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1164
|
+
const continuation = item === undefined ? null : CONTINUATION.exec(line);
|
|
1165
|
+
if (item !== undefined && (fence !== undefined || continuation !== null)) {
|
|
1166
|
+
for (let blank = 0; blank < blanks; blank += 1) item.lines.push("");
|
|
1167
|
+
const text = dedent(line, markerWidth);
|
|
1168
|
+
item.lines.push(text);
|
|
1169
|
+
if (itemPattern === STEP) readFence(text);
|
|
1041
1170
|
blanks = 0;
|
|
1042
1171
|
continue;
|
|
1043
1172
|
}
|
|
1044
1173
|
const group = H3.exec(line);
|
|
1045
1174
|
if (group !== null) {
|
|
1046
|
-
|
|
1175
|
+
closeItem();
|
|
1047
1176
|
groups.push({ name: group[1].trim(), items: [] });
|
|
1048
1177
|
blanks = 0;
|
|
1049
1178
|
continue;
|
|
1050
1179
|
}
|
|
1051
|
-
const
|
|
1052
|
-
if (
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
markerWidth = line.length -
|
|
1056
|
-
|
|
1057
|
-
continue;
|
|
1058
|
-
}
|
|
1059
|
-
const continuation = step === undefined ? null : CONTINUATION.exec(line);
|
|
1060
|
-
if (continuation !== null) {
|
|
1061
|
-
for (let index = 0; index < blanks; index += 1) step.push("");
|
|
1062
|
-
step.push(dedent(line, markerWidth));
|
|
1180
|
+
const first = itemPattern.exec(line);
|
|
1181
|
+
if (first !== null) {
|
|
1182
|
+
closeItem();
|
|
1183
|
+
item = { lines: [first[1]], line: firstLine + index };
|
|
1184
|
+
markerWidth = line.length - first[1].length;
|
|
1185
|
+
if (itemPattern === STEP) readFence(first[1]);
|
|
1063
1186
|
blanks = 0;
|
|
1064
1187
|
continue;
|
|
1065
1188
|
}
|
|
1066
1189
|
throw fail(
|
|
1067
|
-
|
|
1068
|
-
`'${file}' has a line in ${describe} that is not a list item or a '### group' heading
|
|
1190
|
+
"pages.recipe_section_invalid",
|
|
1191
|
+
`'${file}' has a line in ${describe} that is not a list item or a '### group' heading. `
|
|
1069
1192
|
+ `'${line.trim().slice(0, 80)}'.`,
|
|
1070
1193
|
file,
|
|
1071
1194
|
);
|
|
1072
1195
|
}
|
|
1073
|
-
|
|
1196
|
+
closeItem();
|
|
1074
1197
|
return groups.filter((group) => group.items.length > 0 || group.name !== "");
|
|
1075
1198
|
}
|
|
1076
1199
|
|
|
1077
|
-
async function parseRecipeBody(markdown, { file, convert }) {
|
|
1078
|
-
const { lead, sections } = splitSections(markdown);
|
|
1200
|
+
async function parseRecipeBody(markdown, { file, convert, convertStep, firstLine }) {
|
|
1201
|
+
const { lead, sections } = splitSections(markdown, firstLine);
|
|
1079
1202
|
requireSectionTitles(sections, RECIPE_HEADINGS, file, TEMPLATE_RECIPE);
|
|
1080
|
-
const ingredientGroups = groupedLines(sections[0]
|
|
1203
|
+
const ingredientGroups = groupedLines(sections[0], file, BULLET, "the Ingredients section")
|
|
1081
1204
|
.map((group) => ({
|
|
1082
1205
|
name: group.name,
|
|
1083
|
-
ingredients: group.items.map((
|
|
1206
|
+
ingredients: group.items.map(({ text }) => ({ rawText: text.trim() })),
|
|
1084
1207
|
}))
|
|
1085
1208
|
.filter((group) => group.ingredients.length > 0);
|
|
1086
1209
|
const instructionSections = [];
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1210
|
+
const problems = [];
|
|
1211
|
+
for (const { name, items } of groupedLines(sections[1], file, STEP, "the Instructions section")) {
|
|
1212
|
+
if (items.length === 0) continue;
|
|
1213
|
+
const sectionIndex = instructionSections.length;
|
|
1090
1214
|
const stepBodies = [];
|
|
1091
|
-
for (const text of
|
|
1092
|
-
|
|
1215
|
+
for (const [stepIndex, { text, line }] of items.entries()) {
|
|
1216
|
+
const location = { file, name, sectionIndex, stepIndex, firstLine: line };
|
|
1217
|
+
try {
|
|
1218
|
+
const { doc, recipeStepIssues = [] } = await convertStep(text, line);
|
|
1219
|
+
appendRecipeStepProblems(problems, recipeStepIssues.length > 0 ? recipeStepIssues : recipeStepProblems(doc), location);
|
|
1220
|
+
stepBodies.push(doc);
|
|
1221
|
+
} catch (error) {
|
|
1222
|
+
if (!(error instanceof SiteAuthoringError)) throw error;
|
|
1223
|
+
appendRecipeStepProblems(problems, [{
|
|
1224
|
+
line: error.markdownLine,
|
|
1225
|
+
message: error.message.replace(/ \(line \d+\)$/u, ""),
|
|
1226
|
+
}], location);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
instructionSections.push({ name, stepBodies });
|
|
1093
1230
|
}
|
|
1231
|
+
refuseRecipeStepProblems(problems);
|
|
1094
1232
|
return {
|
|
1095
1233
|
introductionBody: lead.trim() === "" ? undefined : await convert(lead),
|
|
1096
1234
|
ingredientGroups,
|
|
@@ -1189,10 +1327,19 @@ export function templateFrontMatterFault(template, fieldNames, file) {
|
|
|
1189
1327
|
* a media reference through the media manifest; both are injected so this
|
|
1190
1328
|
* module stays pure.
|
|
1191
1329
|
*/
|
|
1192
|
-
export async function typedDocumentFromMarkdown({
|
|
1330
|
+
export async function typedDocumentFromMarkdown({
|
|
1331
|
+
template, fields, markdown, file, convert, resolveImage,
|
|
1332
|
+
firstLine = 1,
|
|
1333
|
+
convertStep = async text => ({ doc: await convert(text) }),
|
|
1334
|
+
}) {
|
|
1193
1335
|
if (template === TEMPLATE_GENERATED) throw generatedMarkdownFault(file);
|
|
1194
1336
|
if (template === TEMPLATE_LEGAL) throw legalMarkdownFault(file);
|
|
1337
|
+
const tags = fields.has("tags") ? parseMarkdownTags(fields.get("tags"), file) : undefined;
|
|
1195
1338
|
const displayDate = fields.has("displayDate") ? requireDisplayDate(fields.get("displayDate"), file) : undefined;
|
|
1339
|
+
const displayAuthor = fields.has("displayAuthor") ? requireDisplayAuthor(fields.get("displayAuthor"), file) : undefined;
|
|
1340
|
+
const showOnProfile = fields.has("showOnProfile")
|
|
1341
|
+
? requireShowOnProfile(fields.get("showOnProfile"), file, { fromText: true })
|
|
1342
|
+
: undefined;
|
|
1196
1343
|
let coverImageId;
|
|
1197
1344
|
if (fields.has("coverImage")) {
|
|
1198
1345
|
const reference = fields.get("coverImage");
|
|
@@ -1222,7 +1369,7 @@ export async function typedDocumentFromMarkdown({ template, fields, markdown, fi
|
|
|
1222
1369
|
};
|
|
1223
1370
|
} else {
|
|
1224
1371
|
data = {
|
|
1225
|
-
...(await parseRecipeBody(markdown, { file, convert })),
|
|
1372
|
+
...(await parseRecipeBody(markdown, { file, convert, convertStep, firstLine })),
|
|
1226
1373
|
prepTimeMinutes: parseCount(fields, "prepTimeMinutes", file, MAXIMUM_MINUTES),
|
|
1227
1374
|
cookTimeMinutes: parseCount(fields, "cookTimeMinutes", file, MAXIMUM_MINUTES),
|
|
1228
1375
|
servings: parseCount(fields, "servings", file, MAXIMUM_SERVINGS),
|
|
@@ -1236,13 +1383,25 @@ export async function typedDocumentFromMarkdown({ template, fields, markdown, fi
|
|
|
1236
1383
|
return typedDocumentFromJson(
|
|
1237
1384
|
{
|
|
1238
1385
|
template,
|
|
1239
|
-
...definedEntries([["displayDate", displayDate], ["coverImageId", coverImageId]]),
|
|
1386
|
+
...definedEntries([["displayDate", displayDate], ["displayAuthor", displayAuthor], ["showOnProfile", showOnProfile], ["coverImageId", coverImageId], ["tags", tags]]),
|
|
1240
1387
|
data: withoutUndefined(data),
|
|
1241
1388
|
},
|
|
1242
1389
|
file,
|
|
1243
1390
|
);
|
|
1244
1391
|
}
|
|
1245
1392
|
|
|
1393
|
+
function parseMarkdownTags(value, file) {
|
|
1394
|
+
try {
|
|
1395
|
+
return JSON.parse(value);
|
|
1396
|
+
} catch {
|
|
1397
|
+
throw fail(
|
|
1398
|
+
"pages.tags_invalid",
|
|
1399
|
+
`'${file}' needs a one-line JSON array of tag names, for example tags: ["Trail running", "PNW"].`,
|
|
1400
|
+
"tags",
|
|
1401
|
+
);
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
|
|
1246
1405
|
function withoutUndefined(value) {
|
|
1247
1406
|
return definedEntries(Object.entries(value));
|
|
1248
1407
|
}
|