Skip to content

Commit 001cba8

Browse files
committed
feat: support Text as an allowed child of body
We have a few use cases where we are publishing bodies with text content: - Some articles have trailing text before the body tag closes (we were previously handling this in the from-bodyxml transformer, but it was a hack) - FT Events publish a body without any tags i.e. just text - CCC fallbacks and Clip transcripts currently publish a body with just text There's not a strong reason not to support Text as a valid child of Body (despite the fact Spark doesn't typically allow it), so let's allow it.
1 parent d8ea948 commit 001cba8

File tree

8 files changed

+21
-15
lines changed

8 files changed

+21
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ type BodyBlock =
105105
| Tweet
106106
| Video
107107
| YoutubeVideo
108+
| Text
108109
```
109110
110111
`BodyBlock` nodes are the only things that are valid as the top level of a `Body`.

content-tree.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export declare namespace ContentTree {
2-
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo;
2+
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo | Text;
33
type LayoutWidth = "auto" | "in-line" | "inset-left" | "inset-right" | "full-bleed" | "full-grid" | "mid-grid" | "full-width";
44
type Phrasing = Text | Break | Strong | Emphasis | Strikethrough | Link;
55
interface Node {
@@ -272,7 +272,7 @@ export declare namespace ContentTree {
272272
attributes: CustomCodeComponentAttributes;
273273
}
274274
namespace full {
275-
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo;
275+
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo | Text;
276276
type LayoutWidth = "auto" | "in-line" | "inset-left" | "inset-right" | "full-bleed" | "full-grid" | "mid-grid" | "full-width";
277277
type Phrasing = Text | Break | Strong | Emphasis | Strikethrough | Link;
278278
interface Node {
@@ -546,7 +546,7 @@ export declare namespace ContentTree {
546546
}
547547
}
548548
namespace transit {
549-
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo;
549+
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo | Text;
550550
type LayoutWidth = "auto" | "in-line" | "inset-left" | "inset-right" | "full-bleed" | "full-grid" | "mid-grid" | "full-width";
551551
type Phrasing = Text | Break | Strong | Emphasis | Strikethrough | Link;
552552
interface Node {
@@ -805,7 +805,7 @@ export declare namespace ContentTree {
805805
}
806806
}
807807
namespace loose {
808-
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo;
808+
type BodyBlock = Paragraph | Heading | ImageSet | Flourish | BigNumber | CustomCodeComponent | Layout | List | Blockquote | Pullquote | ScrollyBlock | ThematicBreak | Table | Recommended | Tweet | Video | YoutubeVideo | Text;
809809
type LayoutWidth = "auto" | "in-line" | "inset-left" | "inset-right" | "full-bleed" | "full-grid" | "mid-grid" | "full-width";
810810
type Phrasing = Text | Break | Strong | Emphasis | Strikethrough | Link;
811811
interface Node {

libraries/from-bodyxml/index.js

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -448,19 +448,11 @@ export function fromXast(bodyxast, transformers = defaultTransformers) {
448448
})(bodyxast);
449449
}
450450

451-
/**
452-
* Turns e.g. </p> </body> into </p></body>, which a lot of our content has
453-
* This prevents random strat text nodes being created in bodyTree
454-
* @param {string} xml
455-
* @returns {string}
456-
*/
457-
function removeWhitespaceBeforeBodyTag(xml) {
458-
return xml.replace(/<\/p>\s+<\/body>/gi, '</p></body>');
459-
}
451+
460452

461453
/** @param {string} bodyxml */
462454
export function fromXML(bodyxml) {
463-
return fromXast(xastFromXml(removeWhitespaceBeforeBodyTag(bodyxml)));
455+
return fromXast(xastFromXml(bodyxml));
464456
}
465457

466458
export default fromXML;

schemas/body-tree.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@
119119
},
120120
{
121121
"$ref": "#/definitions/ContentTree.transit.YoutubeVideo"
122+
},
123+
{
124+
"$ref": "#/definitions/ContentTree.transit.Text"
122125
}
123126
]
124127
},

schemas/content-tree.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@
144144
},
145145
{
146146
"$ref": "#/definitions/ContentTree.full.YoutubeVideo"
147+
},
148+
{
149+
"$ref": "#/definitions/ContentTree.full.Text"
147150
}
148151
]
149152
},

schemas/transit-tree.schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@
144144
},
145145
{
146146
"$ref": "#/definitions/ContentTree.transit.YoutubeVideo"
147+
},
148+
{
149+
"$ref": "#/definitions/ContentTree.transit.Text"
147150
}
148151
]
149152
},

tests/bodyxml-to-content-tree/input/simple-old-post.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
<pull-quote-text><p>Maecenas ac ipsum in elit aliquam consectetur. Proin felis metus, efficitur et nulla eu, interdum malesuada diam.</p></pull-quote-text><pull-quote-image><ft-content data-embedded="true" url="http://api-t.ft.com/content/77c8a5b5-c9e3-4df2-ad5f-3ef35fe1d9d4" type="http://www.ft.com/ontology/content/ImageSet"></ft-content></pull-quote-image><pull-quote-source>Pellentesque habitant, morbi tristique</pull-quote-source>
99
</pull-quote>
1010
<p>Donec id faucibus erat. Suspendisse tempor laoreet lorem, sit amet vehicula massa facilisis at. Nulla quis feugiat massa. Praesent viverra non lectus ut ullamcorper. Phasellus <ft-content url="http://api-t.ft.com/content/c71efed9-fe5a-488d-9f47-20c15d177153" type="http://www.ft.com/ontology/content/Article">porttitor neque</ft-content> at volutpat pulvinar.</p>
11-
<p>“Curabitur fermentum, dolor vel interdum varius, tellus justo dapibus velit, interdum sollicitudin dolor nibh varius velit.”</p>
11+
<p>“Curabitur fermentum, dolor vel interdum varius, tellus justo dapibus velit, interdum sollicitudin dolor nibh varius velit.”</p>
1212
</body>

tests/schema/body-tree/valid/kitchen-snippet.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1283,6 +1283,10 @@
12831283
"value": "If this sounds all-too-familiar, stick with me. I have a proven treatment for Kitchen Sink Syndrome."
12841284
}
12851285
]
1286+
},
1287+
{
1288+
"type": "text",
1289+
"value": "",
12861290
}
12871291
]
12881292
}

0 commit comments

Comments
 (0)