MyBB Community Forums

Full Version: Inline jump to post ID if on same page
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So, I'm trying to make a MyCode that takes a post ID as input via parameters and jumps to the post if it's on the same page, only opening a new tab/loading a new page if the post is on a different page.

I tried doing this:

\[post=(.*?)\](.*?)[/post\]
(regex)

<a href="#$1">$2</a>
(string to replace)

but when I actually go to test it on a thread in this forum, where it'll see most of its use, it turns the entire OP of any given thread in there blank without even throwing an error. I have a feeling I've messed something up in the HTML or regex, I just don't know exactly what.

E:

Trying this one:

\[jump tid=(.*?) pid=(.*?)\](.*?)[/jump\]
<a href="http://www.zejroleplaying.org/showthread.php?tid=$1&pid=$2#$2">$3</a>

throws parse errors.
You forgot \ before [

In my board, I changed the way URLs are auto parsed and shortened, to give special treatment to URLs on my own site/forum. It removes the domain name path entirely and prints out the URL directly.

So if someone copy&pastes a link, to, say

http://community.mybb.com/thread-129766-...#pid940595

The way it actually appears in the forum is

thread-129766-post-940595.html#pid940595

Or if you use SEO URLs, it'd appears as

Thread-Inline-jump-to-post-ID-if-on-same-page?pid=940595#pid940595

The ?pid=#pid can't really be helped... users can change the pagination setting in their User CP, and admins and moderators can see moderated posts in a thread. So what is on the same page, or on a specific ?page=x page for one person, may not be there for the other person. Posts may also be deleted (changing the pagination for all following posts), or moved into other threads entirely, breaking links that do not use ?pid=#pid

Thus your mycode that jumps on the same page, while a nice idea, may result in broken links if you do not prevent pagination changes somehow. For a more reliable method, you'd have to add a bit of JavaScript or output filter plugin which looks at the link and remove the domain/path from the a href when it detects that the linked #pid123 anchor is on the same page. This way you could jump on the same page or reload the page depending on necessity
I actually solved that myself just now. Bit of a clunky solution, but it works nonetheless; I ended up adding a seperate MyCode for the case that the TID is NOT included that uses slightly different HTML:

\[jump pid=(.*?)\](.*?)\[/jump\]
<a href="#pid$1">$2</a>

As you can see, I stripped out everything before the anchor in the href, and it works perfectly.
Just had to use this on my forums and this thread helped nudge me in the right direction. So thanks!

For those interested, here's a full tutorial on how to achieve this.

What is it:
The MyCode allows one to jump to a particular section of the same post. This is handy for lengthy posts that may need a table of contents for easier navigation if one chooses not to divide the thread into multiple smaller posts for whatever reason.

How to achieve it:
You need to create two new MyCodes. Go to your admin panel >> Configuration >> MyCode >> Add new MyCode.

Title: InlineJump
Short description: Jump to post segment
Regular expression: \[jump pid=(.*?)\](.*?)\[/jump\]
Replacement: <a href="$1">$2</a>

Save and click to add another MyCode.

Title: Anchor
Short description: Anchor for jump post id.
Regular expression: \[jump=(.*?)\](.*?)\[/jump\]
Replacement: <a id="$1"></a>

Save and that's it.

Example Usage:
[jump pid=#ch1]Chapter 1[/jump]
[jump pid=#ch2]Chapter 2[/jump]
[jump pid=#ch3]Chapter 3[/jump]

Then navigate down to the part of your post where "chapter 1" (or whatever your title is) and put the anchor exactly where you want the page to load at:
[jump=ch1][/jump]

This will allow you to click the "chapter 1" link in the table of contents and instantly scroll down to where you placed the 2nd MyCode. Hope this guide helps someone!