Skip to content

Commit 6193b35

Browse files
authored
Add feature to manipulate title in tablecard on startpage (#1333)
* feat: add table card title transformation utility and tests * feat: implement table card title transformation and update configuration schema * Update pull-request.yml * temp remove dynamic title override * Resetting temporary config an deploy
1 parent 5349f05 commit 6193b35

6 files changed

Lines changed: 100 additions & 6 deletions

File tree

.github/workflows/pull-request.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ name: Build and Deploy
22

33
on:
44
push:
5-
branches: ['main']
5+
branches: ["main"]
66
pull_request:
7-
branches: ['main']
7+
branches: ["main"]
88

99
permissions:
1010
contents: read
@@ -25,7 +25,7 @@ jobs:
2525
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
2626
with:
2727
node-version: ${{ matrix.node-version }}
28-
cache: 'npm'
28+
cache: "npm"
2929

3030
- run: npm ci
3131
- run: npm run build
@@ -45,7 +45,7 @@ jobs:
4545
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
4646
with:
4747
node-version: 24.x
48-
cache: 'npm'
48+
cache: "npm"
4949

5050
- run: npm ci && npm run build && npm run coverage
5151

@@ -96,7 +96,7 @@ jobs:
9696
- uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
9797
with:
9898
node-version: 24.x
99-
cache: 'npm'
99+
cache: "npm"
100100

101101
- run: npm ci && npm run build-artifact
102102

packages/pxweb2/src/app/pages/StartPage/StartPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ import {
6060
} from '../../util/createBreadcrumbItems';
6161
import { createTableListSEO } from '../../util/seo/tableListSEO';
6262
import WipStatusMessage from '../../components/Banners/WipStatusMessage';
63+
import { applyTableCardTitleTransform } from '../../util/tableCardTitleTransform';
6364

6465
const StartPage = () => {
6566
const { t, i18n } = useTranslation();
@@ -433,7 +434,7 @@ const StartPage = () => {
433434
lastPeriodString = table.lastPeriod?.slice(5, 9);
434435
}
435436

436-
const cardTitle = table.label ?? '';
437+
const cardTitle = applyTableCardTitleTransform(table.label ?? '');
437438
const cardUpdatedDate = table.updated
438439
? t('date.simple_date', {
439440
value: new Date(table.updated),

packages/pxweb2/src/app/util/config/config.schema.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@
7676
"type": "boolean",
7777
"description": "Set to true to show breadcrumb on start page"
7878
},
79+
"tableCardTitleTransform": {
80+
"type": "object",
81+
"description": "Optional regex transform applied to start page card titles before rendering.",
82+
"properties": {
83+
"pattern": {
84+
"type": "string",
85+
"description": "Regular expression source string used to match parts of a title."
86+
},
87+
"flags": {
88+
"type": "string",
89+
"description": "Regular expression flags, for example 'g' or 'gi'."
90+
},
91+
"replacement": {
92+
"type": "string",
93+
"description": "Replacement string used with String.replace. Defaults to empty string."
94+
}
95+
},
96+
"required": ["pattern"]
97+
},
7998
"presentationPage": {
8099
"type": "object",
81100
"description": "Configuration for the presentation page",

packages/pxweb2/src/app/util/config/configType.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export type Config = {
1313
showBreadCrumbOnStartPage: boolean;
1414
specialCharacters: string[];
1515
variableFilterExclusionList: { [propName: string]: string[] };
16+
tableCardTitleTransform?: {
17+
pattern: string;
18+
flags?: string;
19+
replacement?: string;
20+
};
1621
homePage?: {
1722
[lang: string]: string;
1823
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { describe, expect, it, vi } from 'vitest';
2+
3+
import { applyTableCardTitleTransform } from './tableCardTitleTransform';
4+
5+
vi.mock('./config/getConfig', () => ({
6+
getConfig: vi.fn(() => ({})),
7+
}));
8+
9+
describe('applyTableCardTitleTransform', () => {
10+
it('returns original title when transform config is missing', () => {
11+
expect(applyTableCardTitleTransform('1234: Example title')).toBe(
12+
'1234: Example title',
13+
);
14+
});
15+
16+
it('applies regex replacement from config', () => {
17+
const result = applyTableCardTitleTransform('1234: Example title', {
18+
pattern: String.raw`^\d+:\s*`,
19+
replacement: '',
20+
});
21+
22+
expect(result).toBe('Example title');
23+
});
24+
25+
it('supports regex flags from config', () => {
26+
const result = applyTableCardTitleTransform('ABC test abc test', {
27+
pattern: 'abc',
28+
flags: 'gi',
29+
replacement: 'X',
30+
});
31+
32+
expect(result).toBe('X test X test');
33+
});
34+
35+
it('returns original title when regex is invalid', () => {
36+
const result = applyTableCardTitleTransform('Title', {
37+
pattern: '[',
38+
replacement: '',
39+
});
40+
41+
expect(result).toBe('Title');
42+
});
43+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { getConfig } from './config/getConfig';
2+
import type { Config } from './config/configType';
3+
4+
type TableCardTitleTransformConfig = NonNullable<
5+
Config['tableCardTitleTransform']
6+
>;
7+
8+
export function applyTableCardTitleTransform(
9+
title: string,
10+
transformConfig?: TableCardTitleTransformConfig,
11+
): string {
12+
const transform = transformConfig ?? getConfig().tableCardTitleTransform;
13+
14+
if (!transform?.pattern) {
15+
return title;
16+
}
17+
18+
try {
19+
return title.replace(
20+
new RegExp(transform.pattern, transform.flags ?? ''),
21+
transform.replacement ?? '',
22+
);
23+
} catch {
24+
return title;
25+
}
26+
}

0 commit comments

Comments
 (0)