Skip to content

Commit

Permalink
fix: replace browser navigation with scroll behavior for internal URL…
Browse files Browse the repository at this point in the history
…s in plugin desc
  • Loading branch information
bajrangCoder committed Nov 4, 2024
1 parent b0367a4 commit 400e204
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/pages/plugin/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import installPlugin from "lib/installPlugin";
import InstallState from "lib/installState";
import settings from "lib/settings";
import markdownIt from "markdown-it";
import anchor from "markdown-it-anchor";
import MarkdownItGitHubAlerts from "markdown-it-github-alerts";
import Url from "utils/Url";
import helpers from "utils/helpers";
Expand Down Expand Up @@ -291,6 +292,13 @@ export default async function PluginInclude(
...plugin,
body: markdownIt({ html: true, xhtmlOut: true })
.use(MarkdownItGitHubAlerts)
.use(anchor, {
slugify: (s) =>
s
.trim()
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-"),
})
.render(plugin.description),
purchased,
installed,
Expand All @@ -305,6 +313,47 @@ export default async function PluginInclude(
minVersionCode,
});

// Handle anchor links
$page.body.querySelectorAll("a[href^='#']").forEach((link) => {
const originalHref = link.getAttribute("href");
link.setAttribute("data-href", originalHref);
link.style.cursor = "pointer";
// Remove default click behavior
link.removeAttribute("href");

// Add custom click handler
link.addEventListener(
"click",
(e) => {
e.preventDefault();
e.stopPropagation();

const hash = link.getAttribute("data-href") || link.textContent;
const targetId = hash.startsWith("#") ? hash.slice(1) : hash;

// Look for either the anchor link or a heading with matching id
const targetElement =
$page.body.querySelector(`[name="${targetId}"]`) ||
$page.body.querySelector(`#${targetId}`);

if (targetElement) {
const headerOffset =
document.querySelector("header")?.offsetHeight || 0;
const elementPosition = targetElement.getBoundingClientRect().top;
const offsetPosition = elementPosition - headerOffset;

$page.body.scrollBy({
top: offsetPosition,
behavior: "smooth",
});
}

return false;
},
{ capture: true },
);
});

if ($settingsIcon) {
$settingsIcon.remove();
$settingsIcon = null;
Expand Down

0 comments on commit 400e204

Please sign in to comment.