Skip to content

Commit

Permalink
fix: always syndicate to Twitter using HTML content property
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Feb 15, 2021
1 parent a3d9bce commit dea22bb
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 46 deletions.
4 changes: 2 additions & 2 deletions fixtures/jf2/all-properties.jf2
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"type": "entry",
"name": "What I had for lunch",
"content": {
"html": "<p>I ate a <em>cheese</em> sandwich, which was nice.</p>",
"text": "I ate a *cheese* sandwich, which was nice."
"html": "<p>I ate a <a href=\"https://en.wikipedia.org/wiki/Cheese\">cheese</a> sandwich, which was nice.</p>",
"text": "I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice."
},
"summary": "A very satisfactory meal.",
"published": "2020-02-02",
Expand Down
2 changes: 1 addition & 1 deletion fixtures/jf2/article-content-provided.jf2
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"type": "entry",
"name": "What I had for lunch",
"content": "I ate a *cheese* sandwich, which was nice."
"content": "I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice."
}
5 changes: 4 additions & 1 deletion fixtures/jf2/checkin.jf2
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "entry",
"content": "I ate a cheese sandwich right here!",
"content": {
"html": "<p>I ate a cheese sandwich right here!</p>",
"text": "I ate a cheese sandwich right here!"
},
"location": {
"properties": {
"latitude": "37.780080",
Expand Down
5 changes: 4 additions & 1 deletion fixtures/jf2/reply.jf2
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "entry",
"content": "I ate a cheese sandwich too!",
"content": {
"html": "<p>I ate a cheese sandwich too!</p>",
"text": "I ate a cheese sandwich too!"
},
"in-reply-to": "https://twitter.com/username/status/1234567890987654321",
"post-type": "reply"
}
5 changes: 4 additions & 1 deletion fixtures/jf2/repost.jf2
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "entry",
"content": "Someone else who likes cheese sandwiches.",
"content": {
"html": "<p>Someone else who likes cheese sandwiches.</p>",
"text": "Someone else who likes cheese sandwiches."
},
"repost-of": "https://twitter.com/username/status/1234567890987654321",
"post-type": "repost"
}
12 changes: 6 additions & 6 deletions packages/endpoint-micropub/tests/unit/jf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ test('Gets content from `content` property', t => {
const result = getContentProperty(properties);

t.deepEqual(result, {
html: '<p>I ate a <em>cheese</em> sandwich, which was nice.</p>',
text: 'I ate a *cheese* sandwich, which was nice.'
html: '<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>',
text: 'I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.'
});
});

Expand Down Expand Up @@ -429,8 +429,8 @@ test('Normalises JF2 (few properties)', t => {
t.is(result.name, 'What I had for lunch');
t.is(result['mp-slug'], 'what-i-had-for-lunch');
t.deepEqual(result.content, {
html: '<p>I ate a <em>cheese</em> sandwich, which was nice.</p>',
text: 'I ate a *cheese* sandwich, which was nice.'
html: '<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>',
text: 'I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.'
});
t.falsy(result.audio);
t.falsy(result.photo);
Expand All @@ -446,8 +446,8 @@ test('Normalises JF2 (all properties)', t => {
t.is(result.type, 'entry');
t.is(result.name, 'What I had for lunch');
t.deepEqual(result.content, {
html: '<p>I ate a <em>cheese</em> sandwich, which was nice.</p>',
text: 'I ate a *cheese* sandwich, which was nice.'
html: '<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>',
text: 'I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.'
});
t.deepEqual(result.audio, [
{url: 'https://website.example/audio.mp3'}
Expand Down
14 changes: 7 additions & 7 deletions packages/preset-hugo/tests/unit/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ test('Renders post template with basic content', t => {
const result = hugo.postTemplate({
published: '2020-02-02',
name: 'What I had for lunch',
content: 'I ate a *cheese* sandwich, which was nice.'
content: 'I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.'
});

t.is(result, `---
date: 2020-02-02
title: What I had for lunch
---
I ate a *cheese* sandwich, which was nice.
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
});

Expand All @@ -47,15 +47,15 @@ test('Renders post template with HTML content', t => {
published: '2020-02-02',
name: 'What I had for lunch',
content: {
html: '<p>I ate a <em>cheese</em> sandwich, which was nice.</p>'
html: '<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>'
}
});

t.is(result, `---
date: 2020-02-02
title: What I had for lunch
---
<p>I ate a <em>cheese</em> sandwich, which was nice.</p>
<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>
`);
});

Expand Down Expand Up @@ -112,7 +112,7 @@ test('Renders post template with JSON frontmatter', t => {
"syndication": "https://website.example/post/12345",
"mp-syndicate-to": "https://social.example"
}
I ate a *cheese* sandwich, which was nice.
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
});

Expand Down Expand Up @@ -156,7 +156,7 @@ url = "https://website.example/photo.jpg"
[[videos]]
url = "https://website.example/video.mp4"
+++
I ate a *cheese* sandwich, which was nice.
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
});

Expand Down Expand Up @@ -199,6 +199,6 @@ visibility: private
syndication: https://website.example/post/12345
mp-syndicate-to: https://social.example
---
I ate a *cheese* sandwich, which was nice.
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
});
10 changes: 5 additions & 5 deletions packages/preset-jekyll/tests/unit/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ test('Renders post template with basic content', t => {
const result = jekyll.postTemplate({
published: '2020-02-02',
name: 'Lunchtime',
content: 'I ate a *cheese* sandwich, which was nice.'
content: 'I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.'
});

t.is(result, `---
date: 2020-02-02
title: Lunchtime
---
I ate a *cheese* sandwich, which was nice.
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
});

Expand All @@ -47,15 +47,15 @@ test('Renders post template with HTML content', t => {
published: '2020-02-02',
name: 'Lunchtime',
content: {
html: '<p>I ate a <em>cheese</em> sandwich, which was nice.</p>'
html: '<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>'
}
});

t.is(result, `---
date: 2020-02-02
title: Lunchtime
---
<p>I ate a <em>cheese</em> sandwich, which was nice.</p>
<p>I ate a <a href="https://en.wikipedia.org/wiki/Cheese">cheese</a> sandwich, which was nice.</p>
`);
});

Expand Down Expand Up @@ -98,6 +98,6 @@ visibility: private
syndication: https://website.example/post/12345
mp-syndicate-to: https://social.example
---
I ate a *cheese* sandwich, which was nice.
I ate a [cheese](https://en.wikipedia.org/wiki/Cheese) sandwich, which was nice.
`);
});
25 changes: 12 additions & 13 deletions packages/syndicator-twitter/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ export const createStatus = (properties, mediaIds = false) => {
const parameters = {};

let status;
let statusText;

// If repost of Twitter URL with content, create a quote tweet
// Else, if post has a non-empty title, show title with a link to post
// Else, if post has plaintext content, use that
// Else, if post has HTML content, convert to plain text and use that
// Else, if post has content, use that
if (properties['repost-of']) {
status = `${properties.content} ${properties['repost-of']}`;
if (properties.content && properties.content.html) {
statusText = htmlToStatusText(properties.content.html);
}

if (statusText && properties['repost-of']) {
// If repost of Twitter URL with content, create a quote tweet
status = `${statusText} ${properties['repost-of']}`;
} else if (properties.name && properties.name !== '') {
// Else, if post has a non-empty title, show title with a link to post
status = `${properties.name} ${properties.url}`;
} else if (properties.content && properties.content.text) {
status = properties.content.text;
} else if (properties.content && properties.content.html) {
status = htmlToStatusText(properties.content.html);
} else if (properties.content) {
status = properties.content;
} else if (statusText) {
// Else, post content (converted to plain text)
status = statusText;
}

// Truncate status if longer than 280 characters
Expand Down
8 changes: 6 additions & 2 deletions packages/syndicator-twitter/tests/unit/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ test('Posts a quote status to Twitter', async t => {
.reply(200, t.context.apiResponse);

const result = await twitter(t.context.options).post({
content: 'Someone else who likes cheese sandwiches.',
content: {
html: '<p>Someone else who likes cheese sandwiches.</p>'
},
'repost-of': t.context.tweetUrl,
'post-type': 'repost'
}, t.context.publication);
Expand Down Expand Up @@ -283,7 +285,9 @@ test('Posts a status to Twitter with 4 out of 5 photos', async t => {
.reply(200, t.context.apiResponse);

const result = await twitter(t.context.options).post({
content: 'Here’s the cheese sandwiches I ate.',
content: {
html: '<p>Here’s the cheese sandwiches I ate.</p>'
},
photo: [
{url: `${t.context.publication.me}image1.jpg`},
{url: `${t.context.publication.me}image2.jpg`},
Expand Down
10 changes: 3 additions & 7 deletions packages/syndicator-twitter/tests/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ test('Creates a status with article post name and URL', t => {
t.is(result.status, 'What I had for lunch https://foo.bar/lunchtime');
});

test('Creates a status with plaintext content', t => {
const result = createStatus(JSON.parse(getFixture('jf2/note-content-provided-html-text.jf2')));

t.is(result.status, 'I ate a cheese sandwich, which was nice.');
});

test('Creates a status with HTML content', t => {
const result = createStatus(JSON.parse(getFixture('jf2/note-content-provided-html.jf2')));

Expand Down Expand Up @@ -66,7 +60,9 @@ test('Creates a status with a location', t => {

test('Creates a status with a photo', t => {
const result = createStatus({
content: 'Here’s the cheese sandwich I ate.'
content: {
html: '<p>Here’s the cheese sandwich I ate.</p>'
}
}, ['1', '2', '3', '4']);

t.is(result.status, 'Here’s the cheese sandwich I ate.');
Expand Down

0 comments on commit dea22bb

Please sign in to comment.