Skip to content

Commit d42edf8

Browse files
committed
Improve Listing Description Generation
-> generalize the rendered content stripping behavior
1 parent e67e3ac commit d42edf8

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

src/project/types/website/listing/website-listing-feed.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,14 @@ import {
2828
kFieldAuthor,
2929
kFieldCategories,
3030
kFieldImage,
31+
kInlineCodeStyle,
3132
kItems,
3233
kListing,
34+
kUrlsToAbsolute,
3335
ListingDescriptor,
3436
ListingFeedOptions,
3537
ListingItem,
38+
RenderedContentOptions,
3639
renderedContentReader,
3740
RenderedContents,
3841
} from "./website-listing-shared.ts";
@@ -232,6 +235,17 @@ export async function createFeed(
232235
return feedFiles;
233236
}
234237

238+
const kFeedOptions: RenderedContentOptions = {
239+
remove: {
240+
anchors: true,
241+
},
242+
transform: {
243+
[kUrlsToAbsolute]: true,
244+
[kInlineCodeStyle]: true,
245+
math: true,
246+
},
247+
};
248+
235249
export function completeStagedFeeds(
236250
context: ProjectContext,
237251
outputFiles: ProjectOutputFile[],
@@ -251,7 +265,11 @@ export function completeStagedFeeds(
251265
// Any feed files for this output file
252266
const files = resolvePathGlobs(dir, [`${stem}${kStagedFileGlob}`], []);
253267

254-
const contentReader = renderedContentReader(context, true, siteUrl);
268+
const contentReader = renderedContentReader(
269+
context,
270+
kFeedOptions,
271+
siteUrl,
272+
);
255273

256274
for (const feedFile of files.include) {
257275
// Info about this feed file

src/project/types/website/listing/website-listing-read.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,9 @@ export function completeListingDescriptions(
272272
outputFiles: ProjectOutputFile[],
273273
_incremental: boolean,
274274
) {
275-
const contentReader = renderedContentReader(context, false);
275+
const contentReader = renderedContentReader(context, {
276+
remove: { links: true, images: true },
277+
});
276278

277279
// Go through any output files and fix up any feeds associated with them
278280
outputFiles.forEach((outputFile) => {

src/project/types/website/listing/website-listing-shared.ts

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,25 @@ export interface RenderedContents {
201201
fullContents: string | undefined;
202202
}
203203

204+
export const kInlineCodeStyle = "inline-code-style";
205+
export const kUrlsToAbsolute = "urls-to-absolute";
206+
207+
export interface RenderedContentOptions {
208+
remove?: {
209+
anchors?: boolean;
210+
links?: boolean;
211+
images?: boolean;
212+
};
213+
transform?: {
214+
[kInlineCodeStyle]?: boolean;
215+
math?: boolean;
216+
[kUrlsToAbsolute]?: boolean;
217+
};
218+
}
219+
204220
export const renderedContentReader = (
205221
project: ProjectContext,
206-
forFeed: boolean,
222+
options: RenderedContentOptions,
207223
siteUrl?: string,
208224
) => {
209225
const renderedContent: Record<string, RenderedContents> = {};
@@ -212,7 +228,7 @@ export const renderedContentReader = (
212228
renderedContent[filePath] = readRenderedContents(
213229
filePath,
214230
project,
215-
forFeed,
231+
options,
216232
siteUrl,
217233
);
218234
}
@@ -238,7 +254,7 @@ export const absoluteUrl = (siteUrl: string, url: string) => {
238254
export function readRenderedContents(
239255
filePath: string,
240256
project: ProjectContext,
241-
forFeed: boolean,
257+
options: RenderedContentOptions,
242258
siteUrl?: string,
243259
): RenderedContents {
244260
const htmlInput = Deno.readTextFileSync(filePath);
@@ -265,7 +281,7 @@ export function readRenderedContents(
265281
}
266282

267283
// Convert any images to have absolute paths
268-
if (forFeed && siteUrl) {
284+
if (options.transform?.[kUrlsToAbsolute] && siteUrl) {
269285
const imgNodes = doc.querySelectorAll("img");
270286
if (imgNodes) {
271287
for (const imgNode of imgNodes) {
@@ -305,16 +321,31 @@ export function readRenderedContents(
305321
});
306322
});
307323

308-
if (forFeed) {
309-
// String unacceptable links
324+
// Strip unacceptable tags
325+
const stripTags = [];
326+
if (options.remove?.images) {
327+
stripTags.push("img");
328+
}
329+
stripTags.forEach((tag) => {
330+
const nodes = doc.querySelectorAll(`${tag}`);
331+
nodes?.forEach((node) => {
332+
const el = node as Element;
333+
el.remove();
334+
});
335+
});
336+
337+
if (options.remove?.anchors) {
338+
// Strip unacceptable links
310339
const relativeLinkSel = 'a[href^="#"]';
311340
const linkNodes = doc.querySelectorAll(relativeLinkSel);
312341
linkNodes.forEach((linkNode) => {
313342
const nodesToMove = linkNode.childNodes;
314343
linkNode.after(...nodesToMove);
315344
linkNode.remove();
316345
});
346+
}
317347

348+
if (options.transform?.[kInlineCodeStyle]) {
318349
// Process code to apply styles for syntax highlighting
319350
const highlightingMap = defaultSyntaxHighlightingClassMap();
320351
const spanNodes = doc.querySelectorAll("code span");
@@ -337,7 +368,9 @@ export function readRenderedContents(
337368
const codeBlockEl = codeBlockNode as Element;
338369
codeBlockEl.setAttribute("style", codeStyle);
339370
}
371+
}
340372

373+
if (options.transform?.math) {
341374
// Process math using webtex
342375
const trimMath = (str: string) => {
343376
// Text of math is prefixed by the below
@@ -359,7 +392,9 @@ export function readRenderedContents(
359392
);
360393
mathNode.parentElement?.replaceChild(imgEl, mathNode);
361394
}
362-
} else {
395+
}
396+
397+
if (options.remove?.links) {
363398
// String all links
364399
const relativeLinkSel = "a";
365400
const linkNodes = doc.querySelectorAll(relativeLinkSel);

0 commit comments

Comments
 (0)