Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Support Vars in HLJS snippets with no spans (#425)
Browse files Browse the repository at this point in the history
Fixes gravitational/teleport#35907

The `rehype-hljs-var` plugin replaces `Var` tags with placeholder
strings, applies syntax highlighting, then substitutes the placeholder
strings with the original `Var` tags. It assumes that each `Var` must be
the child of a `span` tag, though this assumption is not always correct.
This change removes the requirement that the plugin check children of
`span` tags for `Var`s.
  • Loading branch information
ptgott authored Jan 4, 2024
1 parent edea316 commit 800a283
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions server/fixtures/powershell-var.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```powershell
certutil -dspublish -f <Var name="user-ca.cer" /> NTAuthCA
```
2 changes: 2 additions & 0 deletions server/fixtures/result/powershell-var.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre><span class="hljs language-powershell">certutil -dspublish -f </span><var name="user-ca.cer"></var><span class="hljs language-powershell"> NTAuthCA
</span></pre>
1 change: 0 additions & 1 deletion server/rehype-hljs-var.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ export const rehypeVarInHLJS = (
const el = node as Element;
if (
el.type === "element" &&
el.tagName === "span" &&
el.children.length === 1 &&
el.children[0].type === "text"
) {
Expand Down
18 changes: 18 additions & 0 deletions uvu-tests/rehype-hljs-var.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ const transformer = (options: VFileOptions) =>
], // passThrough options says transformer which nodes to leave as is
}) // Transforms remark to rehype
.use(rehypeVarInHLJS, {
aliases: {
bash: ["bsh", "systemd", "code", "powershell"],
},
languages: { hcl: hcl },
})
.use(rehypeMdxToHast)
Expand Down Expand Up @@ -119,6 +122,21 @@ Suite("Insert Var components as HTML nodes: text after a Var", () => {
);
});

Suite("Insert Var components as HTML nodes: powershell snippet", () => {
const result = transformer({
value: readFileSync(resolve("server/fixtures/powershell-var.mdx"), "utf-8"),
path: "/docs/index.mdx",
});

assert.equal(
(result.value as string).trim(),
readFileSync(
resolve("server/fixtures/result/powershell-var.html"),
"utf-8"
).trim()
);
});

Suite("Ignore VarList in code snippet components", () => {
// This throws if the plugin interprets VarList components as being Vars.
transformer({
Expand Down

1 comment on commit 800a283

@vercel
Copy link

@vercel vercel bot commented on 800a283 Jan 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.