Skip to content

Commit 82080af

Browse files
committed
[bilibili.space] Trim empty title and trailing newline
1 parent d8c1efe commit 82080af

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

src/platform/bilibili/source/space.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ mod data {
197197
match self {
198198
Self::None(none) => Some(PostContent::plain(&none.none.tips)),
199199
Self::Opus(opus) => {
200-
if let Some(title) = opus.opus.title.as_deref() {
200+
if let Some(title) = opus.opus.title.as_deref()
201+
&& !title.is_empty()
202+
{
201203
Some(
202204
PostContent::plain(title)
203205
.with_plain("\n\n")
@@ -463,12 +465,21 @@ mod data {
463465
#[derive(Debug, Deserialize)]
464466
pub struct RichText {
465467
rich_text_nodes: Vec<RichTextNode>,
466-
text: String, // Fallback
468+
// text: String, // Fallback
467469
}
468470

469471
impl RichText {
470472
pub fn to_content(&self) -> PostContent {
471-
PostContent::from_parts(self.rich_text_nodes.iter().map(|node| match &node.kind {
473+
// Sometimes the last node is just a newline, we trim it here
474+
let rich_text_nodes = if let Some(last) = self.rich_text_nodes.last()
475+
&& last.is_newline()
476+
{
477+
&self.rich_text_nodes[..self.rich_text_nodes.len() - 1]
478+
} else {
479+
&self.rich_text_nodes[..]
480+
};
481+
482+
PostContent::from_parts(rich_text_nodes.iter().map(|node| match &node.kind {
472483
RichTextNodeKind::Text => PostContentPart::Plain(node.text.clone()),
473484
RichTextNodeKind::Web { jump_url } => PostContentPart::Link {
474485
display: node.text.clone(),
@@ -534,6 +545,14 @@ mod data {
534545
pub kind: RichTextNodeKind,
535546
}
536547

548+
impl RichTextNode {
549+
fn is_newline(&self) -> bool {
550+
matches!(&self.kind, RichTextNodeKind::Text)
551+
&& self.text == "\n"
552+
&& self.orig_text == "\n"
553+
}
554+
}
555+
537556
#[derive(Debug, Deserialize)]
538557
#[serde(tag = "type")]
539558
pub enum RichTextNodeKind {
@@ -761,15 +780,21 @@ fn fetch_space_history_impl<'a>(
761780

762781
fn parse_response(resp: data::SpaceHistory, blocked: &mut BlockedPostIds) -> anyhow::Result<Posts> {
763782
fn parse_item(item: &data::Item, parent_item: Option<&data::Item>) -> anyhow::Result<Post> {
783+
let desc_content = item
784+
.modules
785+
.dynamic
786+
.desc
787+
.as_ref()
788+
.map(|desc| desc.to_content());
764789
let major_content = item
765790
.modules
766791
.dynamic
767792
.major
768793
.as_ref()
769794
.and_then(|major| major.to_content());
770-
let content = match (&item.modules.dynamic.desc, major_content) {
771-
(Some(desc), Some(major)) => desc.to_content().with_plain("\n\n").with_content(major),
772-
(Some(desc), None) => desc.to_content(),
795+
let content = match (desc_content, major_content) {
796+
(Some(desc), Some(major)) => desc.with_plain("\n\n").with_content(major),
797+
(Some(desc), None) => desc,
773798
(None, Some(major)) => major,
774799
(None, None) => bail!("item no content. item: {item:?}"),
775800
};

0 commit comments

Comments
 (0)