Skip to content

Commit d298940

Browse files
authored
Handled completely private posts in ContentPreparer
ref https://linear.app/ghost/issue/AP-961 When we do `if (content === post.html) content = ''` we're secretly doing the removal of member content, for the case where the whole post is member content. When adding extra steps to the content preparation we may do other modifications to the content which should not be done if the post is members only. Instead the method to remove members content will now remove everything if a members content marker isn't found. This means that it should never be called on a public post, this is covered by unit tests in our Post entity.
1 parent 0efe587 commit d298940

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

src/post/post.entity.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,20 +229,16 @@ export class Post extends BaseEntity {
229229
extractLinks: false,
230230
});
231231

232+
if (content === '') {
233+
throw new Error('Cannot create Post from private content');
234+
}
235+
232236
if (
233237
ghostPost.custom_excerpt === null ||
234238
ghostPost.custom_excerpt === ''
235239
) {
236240
excerpt = ContentPreparer.regenerateExcerpt(content);
237241
}
238-
239-
if (content === ghostPost.html) {
240-
content = '';
241-
}
242-
}
243-
244-
if (isPublic === false && content === '') {
245-
throw new Error('Cannot create Post from private content');
246242
}
247243

248244
return new Post(

src/publishing/content.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,6 @@ export class ContentPreparer {
180180
private removeMemberContent(content: string) {
181181
const memberContentIdx = content.indexOf(MEMBER_CONTENT_MARKER);
182182

183-
if (memberContentIdx !== -1) {
184-
return content.substring(0, memberContentIdx);
185-
}
186-
187-
return content;
183+
return content.substring(0, memberContentIdx);
188184
}
189185
}

src/publishing/content.unit.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ describe('ContentPreparer', () => {
2424
expect(result).toEqual('Hello, world!');
2525
});
2626

27+
it('should remove all content if no marker is found', () => {
28+
const content = 'This whole thing is member content';
29+
const result = preparer.prepare(content, {
30+
...allOptionsDisabled,
31+
removeMemberContent: true,
32+
});
33+
34+
expect(result).toEqual('');
35+
});
36+
2737
it('should not remove member by default', () => {
2838
const content = `Hello, world!${MEMBER_CONTENT_MARKER}Member content`;
2939
const result = preparer.prepare(content);

0 commit comments

Comments
 (0)