Skip to content

Improve removal of h-card if it doesn't match page URL #122

@gRegorLove

Description

@gRegorLove

This bit of code is intended to remove h-cards from implicit feeds of h-* when parsing:

// If there is more than one item on the page, it may be a feed.

It appears the logic doesn't work quite right if the h-card has no url property. In this example, the parsed mf2 is (snipped out parts not needed for this example):

"items": [
    {
        "type": [
            "h-entry"
        ],
        "properties": {
            "url": [],
            "published": [],
            "author": [],
            "like-of": []
        },
        "lang": "en"
    },
    {
        "type": [
            "h-card"
        ],
        "properties": {
            "name": [
                "RSS \u00b7 Webmentions"
            ]
        },
        "lang": "en"
    }
],

This is a bit more verbose in part to make it easier to follow, but I think the array_filter should be something like:

$tmpmf2 = array_filter($items, function($item) use($url) {
  if (!in_array('h-card', $item['type'])) {
    // not an h-card; keep it in the result array
    return true;
  }

  if (
    isset($item['properties']['url'][0])
    && $item['properties']['url'][0] == $url
  ) {
    // h-card's u-url matches page URL; keep it in the result array
    return true;
  }

  // h-card u-url does *not* match page URL (or it has no u-url); remove it from result array
  return false;
});

I haven't written a unit test for this yet, otherwise I would make it a PR. It did work in my local testing with the specific example link above.

I might be able to make a PR in the near future, or if someone else wants to take it on, feel free!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions