Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Anchor links to same page shows as broken #894

Closed
1 task
amolswnz opened this issue Apr 19, 2021 · 7 comments
Closed
1 task

Anchor links to same page shows as broken #894

amolswnz opened this issue Apr 19, 2021 · 7 comments

Comments

@amolswnz
Copy link

amolswnz commented Apr 19, 2021

Issue:

Anchor links on internal or same page shows as broken.

My dev setup:

I am able to replicate this issue on basic Silverstripe site
My composer.json

    "require": {
        "php": "^7.1 || ^8",
        "silverstripe/recipe-plugin": "^1.2",
        "silverstripe/recipe-cms": "4.7.3@stable",
        "silverstripe-themes/simple": "~3.2.0",
        "silverstripe/login-forms": "4.3.0@stable",
        "dnadesign/silverstripe-elemental": "^4.5"
    },

To Replicate:

  • In existing or new content block, create an anchor tag
  • Click Insert Link -> Anchor on page
  • Chose above anchor tag
  • Save/Publish Page
  • New Tag is generated as <p><a class="ss-broken" href="[sitetree_link,id=1]#anchor">Link to some anchor</a></p>

Related

#796

ACs

  • Broken anchor on existing pages are not reported as broken links

PRs

Note

  • The current logic for identifying broken anchor is very weak and will report a lot of false positive aside from Elemental blocks. Given that, we think it's better to remove it and only report anchor link broken if the parent page is missing all together
@emteknetnz
Copy link
Member

Thanks for reporting Amol.

Could I get you to quickly find the exact versions of cms and elemental that you're running? You can do that with
composer show | grep '/cms\|/silverstripe-elemental'

Cheers

@amolswnz
Copy link
Author

dnadesign/silverstripe-elemental          4.5.0   Elemental pagetype and collection of Elements
silverstripe/cms                          4.7.1   The SilverStripe Content Management System

@KhushbuFuletra
Copy link

KhushbuFuletra commented Jun 30, 2021

Hi
We are having similar issues. Anchor links on the same page show as broken. Any updates on this?

Just adding my findings below if that helps in debugging the issue further.

Anchors work fine when you select a different page on the "Link to an anchor on a page" popup modal form, but not when you select the same page. (Though doesn't throw any error in the console.)
However, AnchorSelectField::getAnchorsInPage() method does not seem to get called on the initial form load; I'm guessing that the initial set of information in the AnchorSelectField is just loaded from the current WYSIWYG field only and not the whole page.

Adding below list of element module versions and their dependencies.

dnadesign/silverstripe-elemental               4.0.5                                  Elemental pagetype and collection of Elements
dnadesign/silverstripe-elemental-list          1.2.0                                  Adds a new element for nested elements
dnadesign/silverstripe-elemental-userforms     3.0.0                                  Adds a new element for usersforms
silverstripe/cms                               4.7.1                                  The SilverStripe Content Management System
symbiote/silverstripe-elemental-dashboard      1.0.x-dev 7e14217                      Dashboard module with three column elemental area layout
thewebmen/silverstripe-elemental-grid          2.x-dev dc60207                        Elemental grid module

Cheers
Khushbu

@emteknetnz
Copy link
Member

emteknetnz commented Jul 1, 2021

Core issue is that SiteTreeLinkTracking_Parser (which is used by the SiteTreeLinkTracking DataExtension, which DOES run on elemental blocks) assumes that page content is $page->content, while BlockPage elemental content is actually $page .. ElementalAreas() .. Elements() ... HTMLText fields (which are also used in templates)

} elseif (!empty($matches['anchor'])) {
    // Ensure anchor isn't broken on target page
    $anchor = preg_quote($matches['anchor'], '/');
    $broken = !preg_match("/(name|id)=\"{$anchor}\"/", $page->Content); // <<< here's your issue
} 

https://github.com/silverstripe/silverstripe-cms/blob/4/code/Model/SiteTreeLinkTracking_Parser.php#L72

Seems like need to add quite a bit of API to do this properly, something along these lines:

// SiteTree.php
public function getRenderedContent(): string
{
    $renderedContent = [];
    $this->updateRenderedContent($renderedContent);
    $this->extend('augmentRenderedContent', $renderedContent);
    return implode("\n", $renderedContent);
}

// protected to allow subclasses to override this i.e. NOT include $page->Content on BlockPage
protected function updateRenderedContent(array &$renderedContent): void
{
    $renderedContent[] = $this->Content;
}

// BlockPage.php
protected function updateRenderedContent(array &$renderedContent): void
{
   // noop
   // i.e. don't include $this->Content
}

// ElementalPageExtension.php
public function augmentRenderedContent(array &$renderedContent): void
{
    foreach ($this->[ElementalAreas] as $area) {
        foreach ($area->Elements() as $element) {
           foreach ($element->[HTMLText fields] as $field) {
              // this is assuming that ALL HTMLText fields are rendered in templates, should probably have a
              // private static array available on BaseElement  to specify what HTMLText fields are rendered
              $renderedContent[] = $element->$field;
           }
        }
    } 
}

This would allow SiteTreeLinkTracking_Parser to use $page->getRenderedContent() to look for the anchor links

@emteknetnz emteknetnz removed their assignment Jul 2, 2021
@brynwhyman
Copy link

It's perhaps also worth noting there's the dream of a single 'content' API: silverstripe/silverstripe-cms#2454

@emteknetnz
Copy link
Member

Yup, that single content API is pretty much what's required here

@GuySartorelli GuySartorelli self-assigned this May 25, 2022
@GuySartorelli
Copy link
Member

The root problem here is that anchors inside an elemental block are not properly supported. This will be resolved with #186, so I'm closing this issue in favour of that one.

@GuySartorelli GuySartorelli removed their assignment May 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants