Skip to content

Commit ca6ead0

Browse files
authored
Fix word count logic (#1962)
* Fix word count logic * Handle markdown
1 parent ae9abf6 commit ca6ead0

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

packages/experiments-realm/blog-post.gts

+9-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { Author } from './author';
2222
import { formatDatetime, BlogApp as BlogAppCard } from './blog-app';
2323
import { BlogCategory, categoryStyle } from './blog-category';
2424
import { User } from './user';
25+
import { markdownToHtml } from '@cardstack/runtime-common';
2526

2627
class EmbeddedTemplate extends Component<typeof BlogPost> {
2728
<template>
@@ -638,7 +639,14 @@ export class BlogPost extends CardDef {
638639
});
639640
@field wordCount = contains(NumberField, {
640641
computeVia: function (this: BlogPost) {
641-
return this.body?.length;
642+
if (!this.body) {
643+
return 0;
644+
}
645+
const plainText = markdownToHtml(this.body).replace(
646+
/<\/?[^>]+(>|$)/g,
647+
'',
648+
);
649+
return plainText.trim().split(/\s+/).length;
642650
},
643651
});
644652
@field editors = linksToMany(User);

0 commit comments

Comments
 (0)