Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions assets/admin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ import CMS from "@sveltia/cms";

import "./fields.js";
import "./shortcodes.js";
import "./preSave.js";

CMS.init();
70 changes: 70 additions & 0 deletions assets/admin/preSave.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import CMS from "@sveltia/cms";
import * as prettier from "prettier/standalone";
import * as markdownPlugin from "prettier/plugins/markdown";

function isMapLike(value) {
return (
value !== null &&
typeof value === "object" &&
typeof value.get === "function" &&
typeof value.set === "function"
);
}

function convertEscapedNewlinesInTables(markdown) {
// Workaround for https://github.com/facebook/lexical/issues/9323

return markdown
.split("\n")
.map(function (line) {
var trimmed = line.trim();
if (trimmed.startsWith("|") && trimmed.endsWith("|")) {
return line.replace(/(?:\\n)+/g, "<br>");
}
return line;
})
.join("\n");
}

async function formatMarkdown(markdown) {
try {
var formatted = await prettier.format(markdown, {
parser: "markdown",
plugins: [markdownPlugin],
});
var withBreaks = convertEscapedNewlinesInTables(formatted);
return withBreaks.replace(/\n+$/, "");
} catch (error) {
console.error("[preSave] prettier.format failed:", error);
return markdown;
}
}

async function formatBodyField(data) {
if (!isMapLike(data)) {
return data;
}

var body = data.get("body");
if (typeof body !== "string" || !body.trim()) {
return data;
}

var formattedBody = await formatMarkdown(body);
return formattedBody === body ? data : data.set("body", formattedBody);
}

CMS.registerEventListener({
name: "preSave",
handler: async function (args) {
try {
var entry = args.entry;
var data = entry.get("data");
var formattedData = await formatBodyField(data);
return formattedData !== data ? entry.set("data", formattedData) : entry;
} catch (error) {
console.error("[preSave] handler failed:", error);
return args.entry;
}
},
});
48 changes: 47 additions & 1 deletion assets/admin/shortcodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ var highlightFields = [
CMS.registerEditorComponent({
id: "highlight",
label: "Highlight",
icon: "stylus_highlighter",
fields: highlightFields,
pattern: shortcodePattern("highlight", "%"),
fromBlock: function (match) {
Expand All @@ -199,6 +200,7 @@ CMS.registerEditorComponent({
CMS.registerEditorComponent({
id: "highlight-raw",
label: "Highlight",
icon: "stylus_highlighter",
fields: highlightFields,
pattern: shortcodePattern("highlight", "<"),
fromBlock: function (match) {
Expand Down Expand Up @@ -251,6 +253,7 @@ var expanderFields = [
CMS.registerEditorComponent({
id: "expander",
label: "Expander",
icon: "arrow_drop_down_circle",
fields: expanderFields,
pattern: shortcodePattern("expander"),
fromBlock: function (match) {
Expand Down Expand Up @@ -324,6 +327,7 @@ var fipValidityFields = [
CMS.registerEditorComponent({
id: "fip-validity",
label: "FIP Validity Badge",
icon: "verified",
fields: fipValidityFields,
pattern: selfClosingPattern("fip-validity"),
fromBlock: function (match) {
Expand Down Expand Up @@ -443,6 +447,7 @@ var trainCategoryFields = [
CMS.registerEditorComponent({
id: "train-category",
label: "Train Category",
icon: "train",
fields: trainCategoryFields,
pattern: shortcodePattern("train-category"),
fromBlock: function (match) {
Expand Down Expand Up @@ -591,6 +596,7 @@ var bookingFields = [
CMS.registerEditorComponent({
id: "booking",
label: "Booking",
icon: "confirmation_number",
fields: bookingFields,
pattern: dualFormPattern("booking"),
fromBlock: function (match) {
Expand Down Expand Up @@ -653,6 +659,7 @@ var bookingSectionFields = [
CMS.registerEditorComponent({
id: "booking-section",
label: "Booking Section",
icon: "transit_ticket",
fields: bookingSectionFields,
pattern: shortcodePattern("booking-section"),
fromBlock: function (match) {
Expand Down Expand Up @@ -690,6 +697,7 @@ var buttonFields = [
CMS.registerEditorComponent({
id: "button",
label: "Button",
icon: "highlight_mouse_cursor",
fields: buttonFields,
pattern: selfClosingPattern("button"),
fromBlock: function (match) {
Expand Down Expand Up @@ -743,6 +751,7 @@ var identifyOperatorFields = [
CMS.registerEditorComponent({
id: "identify-operator",
label: "Identify Operator",
icon: "search",
fields: identifyOperatorFields,
pattern: dualFormPattern("identify-operator"),
fromBlock: function (match) {
Expand Down Expand Up @@ -780,6 +789,7 @@ var satelliteFields = [
CMS.registerEditorComponent({
id: "satellite",
label: "Satellite Notice",
icon: "satellite_alt",
fields: satelliteFields,
pattern: dualFormPattern("satellite"),
fromBlock: function (match) {
Expand Down Expand Up @@ -819,6 +829,7 @@ var dialogFields = [
CMS.registerEditorComponent({
id: "dialog",
label: "Dialog",
icon: "open_in_browser",
fields: dialogFields,
pattern: shortcodePattern("dialog"),
fromBlock: function (match) {
Expand Down Expand Up @@ -886,6 +897,7 @@ var floatImageFields = [
CMS.registerEditorComponent({
id: "float-image",
label: "Float Image",
icon: "format_image_left",
fields: floatImageFields,
pattern: shortcodePattern("float-image"),
fromBlock: function (match) {
Expand All @@ -912,6 +924,7 @@ CMS.registerEditorComponent({
CMS.registerEditorComponent({
id: "wip",
label: "Work in Progress",
icon: "construction",
fields: [],
pattern: selfClosingPattern("wip"),
fromBlock: function () {
Expand Down Expand Up @@ -955,6 +968,34 @@ CMS.registerEditorComponent({
},
});

CMS.registerEditorComponent({
id: "footnote-reference",
label: "Footnote Reference",
icon: "asterisk",
mode: "dialog",
summary: "Footnote [^{{id}}]",
fields: [
{
name: "id",
label: "Number",
widget: "cms-number-with-usage",
template:
"You need to add the footnote with number {value} at the bottom of the page if it doesn't exist yet.",
required: true,
},
],
pattern: /\[\^([^\]]+)\](?!:)/,
fromBlock: function (match) {
return { id: String(match[1] || "") };
},
toBlock: function (data) {
return "[^" + (data.id || "") + "]";
},
toPreview: function () {
return "";
},
});

var updateFields = [
{
name: "date",
Expand All @@ -975,6 +1016,7 @@ var updateFields = [
CMS.registerEditorComponent({
id: "update",
label: "Update",
icon: "update",
fields: updateFields,
pattern: shortcodePattern("update"),
fromBlock: function (match) {
Expand Down Expand Up @@ -1009,6 +1051,7 @@ var columnFields = [
CMS.registerEditorComponent({
id: "column",
label: "Column",
icon: "vertical_split",
fields: columnFields,
pattern: shortcodePattern("column"),
fromBlock: function (match) {
Expand Down Expand Up @@ -1037,6 +1080,7 @@ var columnsFields = [
CMS.registerEditorComponent({
id: "columns",
label: "Columns",
icon: "view_column",
fields: columnsFields,
pattern: shortcodePattern("columns"),
fromBlock: function (match) {
Expand Down Expand Up @@ -1086,7 +1130,7 @@ var footnoteFields = [
label: "Number",
widget: "cms-number-with-usage",
template:
"You can use this footnote by writing out [^{value}] in the text editor.",
"You can use the footnote by adding footnote reference with number {value} in the text editor.",
param: { required: true },
},
{
Expand All @@ -1106,6 +1150,7 @@ var footnoteFields = [
CMS.registerEditorComponent({
id: "footnote",
label: "Footnote",
icon: "inbox_text_asterisk",
fields: footnoteFields,
pattern: /^\[\^([^\]]+)\]: \[([^\]]*)\]\(([^)]*)\)$/,
fromBlock: function (match) {
Expand Down Expand Up @@ -1136,6 +1181,7 @@ CMS.registerEditorComponent({
CMS.registerEditorComponent({
id: "fip-validity-table",
label: "FIP Validity Table",
icon: "table",
fields: fipValidityTableFields,
pattern: selfClosingPattern("fip-validity-table"),
fromBlock: function (match) {
Expand Down
29 changes: 21 additions & 8 deletions layouts/admin/section.cmsconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,6 @@ collections:
widget: "string"
required: false
i18n: true
- label: "Content Images"
name: "content_images"
widget: "image"
multiple: true
required: false
i18n: duplicate
hint: "Images shown for this page. Prefer .webp images."
accept: "image/webp"
- label: "Countries"
name: "country"
widget: "relation"
Expand All @@ -169,6 +161,14 @@ collections:
required: false
i18n: duplicate
hint: "List of operators this news article is about."
- label: "Content Images"
name: "content_images"
widget: "image"
multiple: true
required: false
i18n: duplicate
hint: "Images shown for this page. Prefer .webp images."
accept: "image/webp"
- label: "Body"
name: "body"
widget: "markdown"
Expand All @@ -182,6 +182,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "update"
- "columns"
- "footnote"
Expand Down Expand Up @@ -238,6 +239,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "identify-operator"
- "columns"
- "footnote"
Expand Down Expand Up @@ -301,6 +303,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -372,6 +375,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -416,18 +420,21 @@ collections:
widget: "markdown"
editor_components:
- "icon"
- "footnote-reference"
minimal: true
- label: "English"
name: "en"
widget: "markdown"
editor_components:
- "icon"
- "footnote-reference"
minimal: true
- label: "Français"
name: "fr"
widget: "markdown"
editor_components:
- "icon"
- "footnote-reference"
minimal: true
- label: "Operators"
name: "operators"
Expand Down Expand Up @@ -697,6 +704,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -729,6 +737,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -767,6 +776,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -805,6 +815,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -841,6 +852,7 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"

Expand Down Expand Up @@ -882,5 +894,6 @@ collections:
- "float-image"
- "wip"
- "icon"
- "footnote-reference"
- "columns"
- "footnote"
Loading
Loading