Skip to content
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

feat: updated default patterns to recognize new patterns #807

Closed
wants to merge 6 commits into from
Closed
82 changes: 79 additions & 3 deletions packages/uiweb/src/lib/components/parsetext/defaultPatterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,65 @@ function renderStyles(matchingString:string) {
return `${match?match[2]:""}`;
}

function renderTextStyles(matchingString: string) {
const pattern = /<PUSHText color=["']?(#[0-9A-Fa-f]{3,6}|[a-zA-Z]+)["']?>(.*?)<\/PUSHText>/i;
const match = matchingString.match(pattern);

if (match) {
const colorName = match[1].toLowerCase();
let color;
switch (colorName) {
case 'primary':
color = COLORS.PRIMARY;
break;
case 'secondary':
color = COLORS.GRADIENT_SECONDARY;
break;
case 'white':
color = COLORS.WHITE;
break;
// can add more custom color names if needed, couldn't find the tertiary color
default:
color = colorName;
}
const textContent = match[2];
return `<span style="color: ${color}">${textContent}</span>`;
}

return matchingString;
}

function renderLinkWithColor(matchingString: string) {
const pattern = /<PUSHText color=["']?(#[0-9A-Fa-f]{3,6}|[a-zA-Z]+)["']?\s+link=["'](https?:\/\/[^"']+)["']>(.*?)<\/PUSHText>/i;
const match = matchingString.match(pattern);

if (match) {
const colorName = match[1].toLowerCase();
let color;
// Map custom color names to specific values
switch (colorName) {
case 'primary':
color = COLORS.PRIMARY;
break;
case 'secondary':
color = COLORS.GRADIENT_SECONDARY;
break;
case 'white':
color = COLORS.WHITE;
break;
// Add more custom color names if needed
default:
color = colorName;
}
const link = match[2];
const textContent = match[3];
return `<a href="${link}" target="_blank" style="color: ${color}">${textContent}</a>`;
}

return matchingString;
}


// -------- Define the required colors
const COLORS = {
PRIMARY: 'rgba(27.0, 150.0, 227.0, 1.0)',
Expand Down Expand Up @@ -140,6 +199,16 @@ const DEFAULT_PATTERNS:CustomParseShape[] = [
},
renderText: renderStyles
},
{
pattern: /<PUSHText color=["']?(#[0-9A-Fa-f]{3,6}|[a-zA-Z]+)["']?>(.*?)<\/PUSHText>/gi,
style: {}, // we can add aditional styles here if needed
renderText: renderTextStyles
},
{
pattern: /<PUSHText color=["']?(#[0-9A-Fa-f]{3,6}|[a-zA-Z]+)["']?\s+link=["'](https?:\/\/[^"']+)["']>(.*?)<\/PUSHText>/gi,
style: {}, // can Add additional styles here if needed
renderText: renderLinkWithColor
},
{
pattern: /\[(up):([^\]]+)\]/i, // url
style: {
Expand Down Expand Up @@ -188,10 +257,17 @@ const DEFAULT_PATTERNS:CustomParseShape[] = [
renderText: renderStyles
},
{
pattern: /\[(i):([^\]]+)\]/i, // italics
style: styles.italics,
renderText: renderStyles
pattern: /\*\*(.*?)\*\*/g, // bold **text**
style: styles.bold,
renderText: (matchingString) => matchingString.replace(/\*\*(.*?)\*\*/g, '$1'),
},
{
pattern: /\*(.*?)\*/g, // italic *some text*
style: {
...styles.italics,
},
renderText: (matchingString) => matchingString.replace(/\*(.*?)\*/g, '$1'),
},
{
pattern: /\[(bi):([^\]]+)\]/i, // bolditalics
style: {
Expand Down