-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgatsby-node.js
More file actions
229 lines (199 loc) · 7.55 KB
/
gatsby-node.js
File metadata and controls
229 lines (199 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const fsExtra = require('fs-extra');
const {
DOC_NAV_PAGE_ID,
NOT_FOUND_PAGE_ID,
VERSION_DROPDOWN,
SITE_URL,
LLMS_SECTIONS,
} = require('./src/configs/doc-configs');
const { getDocLinkFromEdge } = require('./src/utils/gatsby-utils.js');
/* ── Build-time Markdown generation ───────────────────────────────────────
* For every asciidoc node, convert the already-generated HTML to clean
* Markdown using cheerio (DOM pre-processing) + turndown (HTML→MD).
* The result is stored as `fields.markdownBody` on each node and exposed
* in GraphQL so CopyPageDropdown can use it instead of scraping the DOM.
*/
exports.onCreateNode = ({ node, actions }) => {
if (node.internal.type !== 'Asciidoc') return;
const { createNodeField } = actions;
const TurndownService = require('turndown');
const cheerio = require('cheerio');
const html = node.html || '';
const title = node.document?.title || node.pageAttributes?.title || '';
/* Load HTML into cheerio for pre-processing */
const $ = cheerio.load(html, { decodeEntities: false });
/* Remove anchor icon links that Asciidoctor injects next to headings */
$('a.anchor').remove();
/* Remove the embedded TOC — it adds noise to Markdown */
$('#toc').remove();
/* Convert admonition tables to readable text blocks */
$('.admonitionblock').each((_, el) => {
const type = $(el).attr('class').match(/\b(note|tip|warning|caution|important)\b/i)?.[1]?.toUpperCase() || 'NOTE';
const content = $(el).find('td.content').text().trim();
$(el).replaceWith(`<blockquote><p><strong>${type}:</strong> ${content}</p></blockquote>`);
});
/* Get the cleaned HTML */
const cleanedHtml = $('body').html() || '';
/* Configure turndown */
const td = new TurndownService({
headingStyle: 'atx',
bulletListMarker: '-',
codeBlockStyle: 'fenced',
fence: '```',
});
/* GFM table plugin — renders tables as proper Markdown pipe tables */
const { tables } = require('turndown-plugin-gfm');
td.use(tables);
const markdownBody = td.turndown(cleanedHtml);
createNodeField({
node,
name: 'markdownBody',
value: markdownBody,
});
};
exports.onPostBuild = async ({ graphql, reporter }) => {
fsExtra.copyFileSync(
`${__dirname}/robots.txt`,
`${__dirname}/public/robots.txt`,
);
try {
const result = await graphql(`
query {
allAsciidoc {
edges {
node {
document { title }
pageAttributes { pageid }
}
}
}
}
`);
if (result.errors) {
reporter.warn(`llms.txt generation: GraphQL errors — ${JSON.stringify(result.errors)}`);
return;
}
const pageMap = {};
result.data.allAsciidoc.edges.forEach(({ node }) => {
const pageid = node.pageAttributes?.pageid;
const title = node.document?.title;
if (pageid && title) pageMap[pageid] = title;
});
const lines = [
'# ThoughtSpot Developer Documentation',
'',
'> Developer documentation for ThoughtSpot Embedded — tools, APIs, and SDKs for embedding ThoughtSpot analytics into your applications.',
'',
];
for (const section of LLMS_SECTIONS) {
lines.push(`## ${section.label}`);
for (const pageId of section.pageIds) {
const title = pageMap[pageId];
if (title) lines.push(`- [${title}](${SITE_URL}/${pageId})`);
}
lines.push('');
}
fsExtra.writeFileSync(
`${__dirname}/public/llms.txt`,
lines.join('\n'),
);
reporter.info(`llms.txt generated with ${Object.keys(pageMap).length} pages`);
} catch (err) {
reporter.warn(`llms.txt generation failed: ${err.message}`);
}
};
exports.createPages = async function ({ actions, graphql }) {
const { data } = await graphql(`
query {
allAsciidoc {
edges {
node {
html
document {
title
}
pageAttributes {
pageid
}
parent {
... on File {
name
sourceInstanceName
relativePath
}
}
}
}
}
}
`);
const namePageIdMap = {};
// Collect per-category nav HTMLs keyed by category name (pageid minus 'nav-' prefix)
const navMap = {};
const NAV_PARTIAL_PREFIX = 'nav-';
data.allAsciidoc.edges.forEach((e) => {
const {
sourceInstanceName: sourceName,
relativePath: relPath,
} = e.node.parent;
const pageId = e.node.pageAttributes.pageid;
// Collect nav-* files into the navMap (not content pages)
if (pageId && pageId.startsWith(NAV_PARTIAL_PREFIX)) {
navMap[pageId.slice(NAV_PARTIAL_PREFIX.length)] = e.node.html;
return;
}
if (sourceName === 'tutorials') {
const relPathSplit = relPath.split('/');
const pageIdSplit = pageId.split('__');
let finalPageId = pageId;
if (pageIdSplit.length > 1) {
finalPageId = pageIdSplit[1];
}
let mapPageId = `tutorials/${finalPageId}`;
if (relPathSplit.length > 1) {
mapPageId = `tutorials/${relPathSplit[0]}/${finalPageId}`;
}
namePageIdMap[e.node.parent.name] = mapPageId || NOT_FOUND_PAGE_ID;
} else {
namePageIdMap[e.node.parent.name] =
e.node.pageAttributes.pageid || NOT_FOUND_PAGE_ID;
}
});
data.allAsciidoc.edges.forEach((edge) => {
const { pageid: pageId } = edge.node.pageAttributes;
// Skip nav partial files — they are sidebar data, not content pages
if (pageId && pageId.startsWith(NAV_PARTIAL_PREFIX)) return;
const docPath = getDocLinkFromEdge(edge);
actions.createPage({
path: docPath,
component: require.resolve(
'./src/components/DevDocTemplate/index.tsx',
),
context: { pageId, navId: DOC_NAV_PAGE_ID, navMap, namePageIdMap },
});
if (pageId === 'introduction') {
actions.createPage({
path: '/',
component: require.resolve(
'./src/components/DevDocTemplate/index.tsx',
),
context: { pageId, navId: DOC_NAV_PAGE_ID, navMap, namePageIdMap },
});
}
});
VERSION_DROPDOWN.forEach((version) => {
if (version.link === ' ' || !version.iframeUrl) {
return;
}
const versionPath = version.link.startsWith('/')
? version.link.substring(1)
: version.link;
actions.createPage({
path: `/${versionPath}`,
component: require.resolve(
'./src/components/DevDocTemplate/index.tsx',
),
context: { iframeUrl: version.iframeUrl },
});
});
};