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

Adding anchor links to <dt> should not modify Markdown engine #9817

Closed
3 tasks done
wbamberg opened this issue Oct 13, 2023 · 5 comments · Fixed by #9862
Closed
3 tasks done

Adding anchor links to <dt> should not modify Markdown engine #9817

wbamberg opened this issue Oct 13, 2023 · 5 comments · Fixed by #9862
Assignees
Labels
effort: small This task is a small effort. p2 We want to address this but may have other higher priority items. refactor Refactorings

Comments

@wbamberg
Copy link
Collaborator

Summary

#8413 added anchor links to <dt> elements, which is nice. But it did it inside the Markdown engine, which makes the Markdown output less clean. Ideally the Markdown engine should just be as close to standard GFM as we can get, and Yari should add extra features like this on top of the HTML output.

(For example, we also add anchor links to headings in Yari, but we don't do it by modifying the Markdown engine.)

See also the discussion in #8413 (comment).

URL

Any MDN page that includes a <dl>, such as https://developer.mozilla.org/en-US/docs/Web/CSS/margin.

Reproduction steps

Have Yari build a page from Markdown.

Expected behavior

Markdown engine should just generate plain <dl> structures from the <dl> syntax we have invented, and any additions to that should be done on top of that output.

Actual behavior

Markdown engine generates <dl> structures that include anchors.

Device

Desktop

Browser

Chrome

Browser version

Stable

Operating system

Mac OS

Screenshot

No response

Anything else?

No response

Validations

@github-actions github-actions bot added the needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. label Oct 13, 2023
@KartikSoneji
Copy link
Contributor

Looks like this is enough to add hyperlinks to <dt> elements, with 0 modifications to the markdown engine.
It looks like it produces equivalent output on the subset of files I tested but ideally the current version and this would be tested on a full build of the site, if they're identical then we can switch to this version.

diff --git a/kumascript/src/api/util.ts b/kumascript/src/api/util.ts
index 0e7f0c2e9..6cda399a0 100644
--- a/kumascript/src/api/util.ts
+++ b/kumascript/src/api/util.ts
@@ -223,16 +223,8 @@ export class HTMLTool {
       knownIDs.add(id);
       $element.attr("id", id);
 
-      if (isDt) {
-        // Remove empty anchor links.
-        // This happens if the term already links to a page.
-        $element.find("a[data-link-to-id = true]:empty").remove();
-
-        // Link remaining anchor links to the term's ID.
-        $element
-          .find("a[data-link-to-id = true]")
-          .attr("href", "#" + id)
-          .removeAttr("data-link-to-id");
+      if (isDt && $element.is("a:not([href])")) {
+        $element.attr("href", "#" + id);
       }
     });
     return this;
diff --git a/markdown/m2h/handlers/dl.ts b/markdown/m2h/handlers/dl.ts
index 6aca68c03..252356a1d 100644
--- a/markdown/m2h/handlers/dl.ts
+++ b/markdown/m2h/handlers/dl.ts
@@ -38,21 +38,19 @@ export function asDefinitionList(h, node) {
       DEFINITION_PREFIX.length
     );
 
-    const [firstDtChild, ...dtChildren] = all(h, {
-      ...node,
-      children:
-        terms.length == 1 && terms[0].type == "paragraph"
-          ? terms[0].children
-          : terms,
-    });
-    if (firstDtChild) {
-      dtChildren.unshift(
-        h(node, "a", { "data-link-to-id": "true" }, [firstDtChild])
-      );
-    }
-
     return [
-      h(node, "dt", {}, dtChildren),
+      h(
+        node,
+        "dt",
+        {},
+        all(h, {
+          ...node,
+          children:
+            terms.length == 1 && terms[0].type == "paragraph"
+              ? terms[0].children
+              : terms,
+        })
+      ),
       h(
         node,
         "dd",

@caugner caugner added p2 We want to address this but may have other higher priority items. refactor Refactorings effort: small This task is a small effort. and removed needs triage Triage needed by staff and/or partners. Automatically applied when an issue is opened. labels Oct 16, 2023
@caugner
Copy link
Contributor

caugner commented Oct 16, 2023

@KartikSoneji Would you mind opening a PR for these changes? 🙏

@KartikSoneji
Copy link
Contributor

@caugner I would be happy to, but can someone please confirm if at least the en-us repo has identical outputs with the current and new version? I wouldn't want to make a hasty change that results in a regression.
Last time I tried a full build it timed out GitPod, or I can try on my local machine overnight.

@caugner
Copy link
Contributor

caugner commented Oct 17, 2023

@KartikSoneji Can you open the PR? Then I can easily run two full builds locally and compare.

@KartikSoneji
Copy link
Contributor

@caugner done: #9862

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
effort: small This task is a small effort. p2 We want to address this but may have other higher priority items. refactor Refactorings
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants