-
Notifications
You must be signed in to change notification settings - Fork 3
feat: Add code blocks transformation to Confluence code macro #66
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
Merged
javierbrea
merged 2 commits into
Telefonica:feat/65/rehype-codeblocks
from
FrankLedo:feat/65/code-blocks-confluence-macro
Nov 17, 2025
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
140 changes: 140 additions & 0 deletions
140
...nfluence-sync/src/lib/confluence/transformer/support/rehype/rehype-replace-code-blocks.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| // SPDX-FileCopyrightText: 2025 Telefónica Innovación Digital | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| import type { Element as HastElement, Root, Text as HastText } from "hast"; | ||
| import type { Plugin as UnifiedPlugin } from "unified"; | ||
|
|
||
| import { replace } from "../../../../support/unist/unist-util-replace.js"; | ||
|
|
||
| /** | ||
| * UnifiedPlugin to replace `<pre><code>` HastElements with Confluence's | ||
| * structured code macro format. | ||
| * | ||
| * @see {@link https://developer.atlassian.com/server/confluence/confluence-storage-format/ | Confluence Storage Format } | ||
| * | ||
| * @example | ||
| * <pre><code class="language-javascript">const x = 42;</code></pre> | ||
| * // becomes | ||
| * <ac:structured-macro ac:name="code"> | ||
| * <ac:parameter ac:name="language">javascript</ac:parameter> | ||
| * <ac:plain-text-body><![CDATA[const x = 42;]]></ac:plain-text-body> | ||
| * </ac:structured-macro> | ||
| */ | ||
| const rehypeReplaceCodeBlocks: UnifiedPlugin<[], Root> = | ||
| function rehypeReplaceCodeBlocks() { | ||
| return function transformer(tree) { | ||
| replace(tree, { type: "element", tagName: "pre" }, (node) => { | ||
| // Check if this pre element contains a code element | ||
| const codeElement = node.children.find( | ||
| (child) => | ||
| child.type === "element" && | ||
| (child as HastElement).tagName === "code", | ||
| ) as HastElement | undefined; | ||
|
|
||
| if (!codeElement) { | ||
| // If there's no code element, return the pre element unchanged | ||
| return node; | ||
| } | ||
|
|
||
| // Extract the language from the code element's className | ||
| const language = extractLanguage(codeElement); | ||
|
|
||
| // Extract the text content from the code element | ||
| const codeContent = extractTextContent(codeElement); | ||
|
|
||
| // Build the Confluence code macro | ||
| const macroChildren: HastElement[] = []; | ||
|
|
||
| // Add language parameter if present | ||
| if (language) { | ||
| macroChildren.push({ | ||
| type: "element" as const, | ||
| tagName: "ac:parameter", | ||
| properties: { | ||
| "ac:name": "language", | ||
| }, | ||
| children: [ | ||
| { | ||
| type: "text" as const, | ||
| value: language, | ||
| }, | ||
| ], | ||
| }); | ||
| } | ||
|
|
||
| // Add the code content | ||
| // Note: We use a text node with the raw CDATA markup | ||
| // The rehypeStringify with allowDangerousHtml will preserve it | ||
| macroChildren.push({ | ||
| type: "element" as const, | ||
| tagName: "ac:plain-text-body", | ||
| properties: {}, | ||
| children: [ | ||
| { | ||
| type: "text" as const, | ||
| value: `<![CDATA[${codeContent}]]>`, | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| return { | ||
| type: "element" as const, | ||
| tagName: "ac:structured-macro", | ||
| properties: { | ||
| "ac:name": "code", | ||
| }, | ||
| children: macroChildren, | ||
| }; | ||
| }); | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Extract the language from the code element's className property. | ||
| * Markdown renderers typically add classes like "language-javascript" | ||
| * to code elements. | ||
| * | ||
| * @param codeElement - The code element to extract the language from | ||
| * @returns The language identifier or undefined if not found | ||
| */ | ||
| function extractLanguage(codeElement: HastElement): string | undefined { | ||
| const className = codeElement.properties?.className; | ||
|
|
||
| if (!className) { | ||
| return undefined; | ||
| } | ||
|
|
||
| // className can be a string or an array of strings | ||
| const classNames = Array.isArray(className) ? className : [className]; | ||
|
|
||
| // Look for a class that starts with "language-" | ||
| for (const cls of classNames) { | ||
| if (typeof cls === "string" && cls.startsWith("language-")) { | ||
| return cls.substring(9); // Remove "language-" prefix | ||
| } | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Extract all text content from an element recursively. | ||
| * | ||
| * @param element - The element to extract text from | ||
| * @returns The concatenated text content | ||
| */ | ||
| function extractTextContent(element: HastElement): string { | ||
| let text = ""; | ||
|
|
||
| for (const child of element.children) { | ||
| if (child.type === "text") { | ||
| text += (child as HastText).value; | ||
| } else if (child.type === "element") { | ||
| text += extractTextContent(child as HastElement); | ||
| } | ||
| } | ||
|
|
||
| return text; | ||
| } | ||
|
|
||
| export default rehypeReplaceCodeBlocks; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.