Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ const { config } = require('./apify-docs-theme');
const { collectSlugs } = require('./tools/utils/collectSlugs');
const { externalLinkProcessor, isInternal } = require('./tools/utils/externalLink');
const { removeLlmButtons } = require('./tools/utils/removeLlmButtons');
const { rehypeExpandTabs } = require('./tools/utils/rehypeExpandTabs');
const { rehypeFixCodeLanguage } = require('./tools/utils/rehypeFixCodeLanguage');

/**
* Helper to extract text from a node recursively.
Expand Down Expand Up @@ -354,6 +356,8 @@ module.exports = {
categoryName: 'Platform documentation',
},
],
// Expand <Tabs> and fix Prism code language tags before HTML→markdown conversion
beforeDefaultRehypePlugins: [rehypeExpandTabs, rehypeFixCodeLanguage],
// Add custom remark processing to remove LLM button text
remarkPlugins: [removeLlmButtons],
},
Expand Down
70 changes: 70 additions & 0 deletions tools/utils/rehypeExpandTabs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict';

const { visit, SKIP } = require('unist-util-visit');
const { selectAll } = require('hast-util-select');
const { toString } = require('hast-util-to-string');
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should these be added to package.json?


function isTabsContainer(node) {
return (
node.tagName === 'div' &&
Array.isArray(node.properties?.className) &&
node.properties.className.includes('tabs-container')
);
}

function buildReplacement(tabsContainer) {
const labels = selectAll('ul[role="tablist"] li[role="tab"]', tabsContainer).map((li) => toString(li).trim());
const panels = selectAll('div[role="tabpanel"]', tabsContainer);

const result = [];
for (let i = 0; i < panels.length; i++) {
const panel = panels[i];
if (panel.properties) delete panel.properties.hidden;

if (labels[i]) {
result.push({
type: 'element',
tagName: 'p',
properties: {},
children: [
{
type: 'element',
tagName: 'strong',
properties: {},
children: [{ type: 'text', value: labels[i] }],
},
],
});
}

result.push(...(panel.children ?? []));
}
return result;
}

/**
* Expands Docusaurus <Tabs> into sequential labeled sections before HTML→markdown conversion.
*
* Without this, the pipeline drops hidden tab panels and loses label-to-content
* associations, making multi-language examples unreadable for LLMs.
*/
function rehypeExpandTabs() {
return (tree) => {
const replacements = [];

visit(tree, 'element', (node, index, parent) => {
if (!isTabsContainer(node)) return;
const nodes = buildReplacement(node);
if (nodes.length === 0) return;
replacements.push({ parent, index, nodes });
return SKIP;
});

// Reverse to preserve indices when splicing
for (const { parent, index, nodes } of replacements.reverse()) {
parent.children.splice(index, 1, ...nodes);
}
};
}

module.exports = { rehypeExpandTabs };
33 changes: 33 additions & 0 deletions tools/utils/rehypeFixCodeLanguage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const { visit } = require('unist-util-visit');

/**
* Copies the `language-*` class from Prism's `<pre>` to its child `<code>` element.
*
* Docusaurus renders code blocks with the language class on `<pre class="prism-code
* language-javascript">` but `hast-util-to-mdast` only checks `<code>` for the class,
* so without this all code blocks lose their language tag in generated markdown.
*/
function rehypeFixCodeLanguage() {
return (tree) => {
visit(tree, 'element', (node) => {
if (node.tagName !== 'pre') return;

const preClasses = node.properties?.className ?? [];
const langClass = preClasses.find((c) => typeof c === 'string' && c.startsWith('language-'));
if (!langClass) return;

const code = (node.children ?? []).find((c) => c.type === 'element' && c.tagName === 'code');
if (!code) return;

if (!code.properties) code.properties = {};
const codeClasses = code.properties.className ?? [];
if (!codeClasses.some((c) => typeof c === 'string' && c.startsWith('language-'))) {
code.properties.className = [langClass, ...codeClasses];
}
});
};
}

module.exports = { rehypeFixCodeLanguage };
Loading