TinyMCE Rich Text Editor - Anchors - What am I missing?

Hi all,

I feel like I’m missing something simple here. Wouldn’t be the first time!

When adding an anchor [eg, #anchor1] to a non-home page [eg, about] via the TinyMCE RTE link editor - the resultant link points to:

https://domain.com#anchor1

instead of:

https://domain.com/about#anchor1

So all anchors created point back to the home page.

This happens whether you generate the anchors in the TinyMCE RTE link editor or by manually inserting them in the Content field.

The following tag exists on all pages:

<base href="https://domain.com/" />

I know I could override this by pointing to [[~[[*id]]]]#anchor1 but I can’t expect users to do this.

Thanks in advance for any thoughts.

Chris

EDIT

accidentally prematurely closed the topic before I posted my thanks to @jako for his solution:


@jako - apologies I’ve only just got round to looking properly at this.

Your solution works great.

My final code is as follows and the plugin fires on the OnWebPagePrerender system event:

if ($modx->resource->get('id') != $modx->config['site_start']) {
   $modx->resource->_output = preg_replace('/(href=(\'|"))(#.*?\2)/', '$1' . rtrim($modx->makeUrl($modx->resource->get('id'), "", "", "full"), '/') . '$3', $modx->resource->_output);
}

Thanks as always for your help!

:+1:

Chris

You could remove the base tag from your template

<base href="[[!++site_url]]" />

But then you probably have to change the system setting link_tag_scheme, so that all your other links are not relative anymore.

Thanks as always @halftrainedharry

I’m not quite sure of all of the implications of doing that.

I’ll maybe need to try a staging site and mess with it a bit.

But - does this mean that out of the box - the TinyMCE RTE anchor functionality just won’t work if you declare your base URL?

MODX ships with a demo template with <base href="[[!++site_url]]"> in place and I’m fairly sure I’ve found myself in trouble in the past when I’ve omitted that tag from sites.

I realise that this is only partially a MODX issue - but I’d like to know how others approach this?

There exists an anchor plugin from the old forums which should run on OnWebPagePrerender

The code can be improved a bit to:

if ($modx->resource->get('id') != $modx->config['site_start']) {
   $modx->resource->_output = preg_replace('/(href=(\'|"))(#.*?\2)/', '$1' . $modx->makeUrl($modx->resource->get('id'), "", "", "full") . '$3', $modx->resource->_output);
}

Maybe the trailing slash in the forum code is necessary and has to be stripped away. Then there has to be a rtrim added around the makeUrl: rtrim($modx->makeUrl($modx->resource->get('id'), "", "", "full"), '/')

1 Like

Thanks for your thoughts @jako

I will look at this - but I kind of feel that the expected behaviour of TinyMCE RTE & MODX would be to manage this or at least provide options for the developer to manage it. The link editor in TinyMCE is really quite clever, creating anchors based on your search for content within the resource. But the links just wont work where the base URL is specified.

Always grateful for the input!

The plugin just creates full url + anchor links from anchor links during OnWebPagePrerender and solves the base_url issue that way. It does the same what you can’t expect users to do.

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.