Skip to content

Commit

Permalink
Rework reference one line description extraction function
Browse files Browse the repository at this point in the history
  • Loading branch information
limzykenneth committed Jan 31, 2025
1 parent 8d3ab19 commit 1ea2306
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
22 changes: 15 additions & 7 deletions src/components/ReferenceDirectoryWithFilter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,21 @@ type ReferenceDirectoryWithFilterProps = {
* @returns One-line description
*/
const getOneLineDescription = (description: string): string => {
// Matches until the first ., ?, !, ।, or 。 followed by a space
const fullStopRegex = /.*?(?:\.\s|\?\s|!\s|\s|\s)/;
const cleanedDescription = description
.replace(/<[^>]*>?/gm, "")
.replace(/\n/g, " ");
const [oneLineDescription] = cleanedDescription.match(fullStopRegex) ?? [];
return `${oneLineDescription?.trim() ?? cleanedDescription}`;
// Matches first paragraph tag, remove HTML tags, then trim to first fullstop
const firstParagraphRegex = /^<p>(.*?)<\/p>/;
let [oneLineDescription] = description.replace(/\n/g, " ").trim()
.match(firstParagraphRegex) ?? [];

if(oneLineDescription){
oneLineDescription = oneLineDescription
.replace(/^<p>|<\/p>$/g, "")
.replace(/<\/?code>/g, "")
.replace(/<var>(\d+?)<sup>(\d+?)<\/sup><\/var>/g, "$1^$2")
.replace(/<a href=".*?">|<\/a>/g, "")
.split(/\.\s|\?\s|!\s|\s|/)[0];
}

return oneLineDescription ?? "";
};

export const ReferenceDirectoryWithFilter = ({
Expand Down
2 changes: 1 addition & 1 deletion src/content/reference/en/p5/createP.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module: DOM
submodule: DOM
file: src/dom/dom.js
description: >
<p>Creates a <code><p></p></code> element.</p>
<p>Creates a paragraph element.</p>
<p><code><p></p></code> elements are commonly used for paragraph-length
text.</p>
Expand Down

0 comments on commit 1ea2306

Please sign in to comment.