Skip to content

Commit ce4eb3d

Browse files
authored
Merge pull request #1 from charlie-tango/main
Sync wtih base repo
2 parents 391e229 + 2d77c00 commit ce4eb3d

31 files changed

+3961
-10599
lines changed

.github/workflows/ci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ jobs:
1313
build:
1414
runs-on: ubuntu-latest
1515
steps:
16+
- run: npm install --global corepack@latest
1617
- run: corepack enable
1718
- uses: actions/checkout@v4
1819
- name: Setup Node.js
@@ -24,6 +25,8 @@ jobs:
2425
run: pnpm install
2526
- name: Lint
2627
run: pnpm biome ci .
28+
- name: Install playwright
29+
run: pnpx playwright install chromium
2730
- name: Test
2831
run: pnpm test
2932
env:

.github/workflows/pkg-pr.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Publish Pull Requests
2+
on: [push, pull_request]
3+
4+
jobs:
5+
pr-package:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- run: npm install --global corepack@latest
9+
- run: corepack enable
10+
- uses: actions/checkout@v4
11+
- name: Setup Node.js
12+
uses: actions/setup-node@v4
13+
with:
14+
node-version: 20
15+
cache: "pnpm"
16+
- name: Install dependencies
17+
run: pnpm install
18+
- name: Build
19+
run: pnpm build
20+
- name: Publish preview package
21+
run: pnpx pkg-pr-new publish --no-template --compact

.github/workflows/release.yml

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
release:
1313
runs-on: ubuntu-latest
1414
steps:
15+
- run: npm install --global corepack@latest
1516
- run: corepack enable
1617
- uses: actions/checkout@v4
1718
with:

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ lib-cov
99
logs
1010
node_modules
1111
temp
12+
__screenshots__

README.md

+52-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ choice.
1515
npm install @charlietango/react-umbraco
1616
```
1717

18-
### `<UmbracoRichText>`
18+
## `<UmbracoRichText>`
1919

20-
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz_small.svg)](https://stackblitz.com/github/charlie-tango/react-umbraco/tree/main/examples/UmbracoRichText?file=src/RichText.tsx)
20+
[![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz_small.svg)](https://stackblitz.com/github/charlie-tango/react-umbraco/tree/main?file=examples/UmbracoRichText/src/RichText.tsx)
2121

2222
Takes the rich text property from the Umbraco Content Delivery API and renders
2323
it with React.
@@ -28,6 +28,10 @@ it with React.
2828
- `renderBlock`: Render a specific block type.
2929
- `renderNode`: Overwrite the default rendering of a node. Return `undefined` to
3030
render the default node. Return `null` to skip rendering the node.
31+
- `htmlAttributes`: Default attributes to set on the defined HTML elements.
32+
These will be used, unless the element already has the attribute set. The only
33+
exception is the `className` attribute, which will be merged with the default
34+
value.
3135

3236
When passing the `renderBlock` and `renderNode` props, consider making them
3337
static functions (move them outside the consuming component) to avoid
@@ -74,12 +78,13 @@ function RichText({ data }) {
7478
element={data.richText}
7579
renderNode={renderNode}
7680
renderBlock={renderBlock}
81+
htmlAttributes={{ p: { className: "mb-4" }}
7782
/>
7883
);
7984
}
8085
```
8186

82-
#### Blocks
87+
### Blocks
8388

8489
You can augment the `renderBlock` method with the generated OpenAPI types from
8590
Umbraco Content Delivery API. That way you can correctly filter the blocks you
@@ -105,6 +110,50 @@ declare module "@charlietango/react-umbraco" {
105110
}
106111
```
107112

113+
## `richTextToPlainText`
114+
115+
A utility function to convert an Umbraco RichText element to plain text. This
116+
can be useful for generating meta descriptions or other text-based properties.
117+
118+
### Parameters
119+
120+
- `data` (`RichTextElementModel`): The rich text element to be converted.
121+
- `options` (`Options`, _optional_): An object to specify additional options.
122+
- `firstParagraph` (`boolean`, _optional_): If `true`, only the first
123+
paragraph with text content will be returned.
124+
- `maxLength` (`number`, _optional_): The maximum length of the returned text.
125+
If the text exceeds this length, it will be truncated to the nearest word
126+
and an ellipsis will be added.
127+
- `ignoreTags` (`Array<string>`, _optional_): An array of tags to be ignored
128+
during the conversion.
129+
130+
### Returns
131+
132+
- `string`: The plain text representation of the rich text element.
133+
134+
### Example
135+
136+
```ts
137+
import { richTextToPlainText } from "@charlietango/react-umbraco";
138+
139+
const plainText = richTextToPlainText(richTextData);
140+
141+
// Just the first paragraph
142+
const firstParagraph = richTextToPlainText(richTextData, {
143+
firstParagraph: true,
144+
});
145+
146+
// Just the first 100 characters, truncated at the nearest word with an ellipsis
147+
const first100Characters = richTextToPlainText(richTextData, {
148+
maxLength: 100,
149+
});
150+
151+
// Ignore certain tags, skipping their content
152+
const ignoreTags = richTextToPlainText(richTextData, {
153+
ignoreTags: ["h1", "h2", "ol", "figure"],
154+
});
155+
```
156+
108157
<!-- Badges -->
109158

110159
[npm-version-src]:

biome.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"coverage/**",
99
"storybook-static/**",
1010
"pnpm-lock.yaml",
11-
"examples/**"
11+
"examples/**",
12+
"package.json"
1213
]
1314
},
1415
"organizeImports": {

examples/UmbracoRichText/.eslintrc.cjs

-18
This file was deleted.
File renamed without changes.

examples/UmbracoRichText/README.md

-39
This file was deleted.

0 commit comments

Comments
 (0)