Skip to content

Commit 9d9e565

Browse files
authored
Merge pull request #367 from scientist-softserv/attempt-markdown
Get markdown working on browse pages
2 parents fe10ae8 + b4ca394 commit 9d9e565

File tree

5 files changed

+676
-17
lines changed

5 files changed

+676
-17
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"next-auth": "^4.20.1",
3434
"react": "18.2.0",
3535
"react-dom": "18.2.0",
36+
"react-markdown": "^9.0.1",
3637
"sass": "^1.56.1",
3738
"start-server-and-test": "^2.0.3",
3839
"swr": "^1.3.0"

pages/browse/index.js

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,21 @@ import {
66
Notice,
77
SearchBar,
88
} from '@scientist-softserv/webstore-component-library'
9+
import Markdown from 'react-markdown'
910
import {
1011
buttonBg,
1112
configureErrors,
1213
configureServices,
1314
useFilteredWares,
15+
truncateDescription,
1416
} from '../../utils'
1517

1618
const Browse = ({ session }) => {
1719
const router = useRouter()
1820
const [query, setQuery] = useState('')
1921
const existingQuery = router.query.q
2022
const accessToken = session?.accessToken
23+
const [open, setOpen] = useState(false)
2124

2225
useEffect(() => {
2326
if (existingQuery) setQuery(existingQuery)
@@ -60,19 +63,32 @@ const Browse = ({ session }) => {
6063
<>
6164
{(services.length > 0) ? (
6265
<>
63-
{services.map(service => (
64-
<Item
65-
key={service.id}
66-
item={service}
67-
withButtonLink={true}
68-
buttonLink={service.href}
69-
orientation='horizontal'
70-
buttonProps={{
71-
backgroundColor: buttonBg,
72-
label: 'Request this item',
73-
}}
74-
/>
75-
))}
66+
{services.map(service => {
67+
const { truncated, cutOffIndex } = truncateDescription(service?.description, 300, open)
68+
return (
69+
<Item
70+
key={service.id}
71+
markdownDescriptionTruncated={(
72+
<Markdown>
73+
{truncated}
74+
</Markdown>
75+
)}
76+
markdownDescriptionExtended={(
77+
<Markdown>
78+
{service?.description?.slice(cutOffIndex).trimStart()}
79+
</Markdown>
80+
)}
81+
item={service}
82+
withButtonLink={true}
83+
buttonLink={service.href}
84+
orientation='horizontal'
85+
buttonProps={{
86+
backgroundColor: buttonBg,
87+
label: 'Request this item',
88+
}}
89+
/>
90+
)
91+
})}
7692
</>
7793
) : (
7894
<p data-cy='no-results'>

utils/api/configurations.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const configureServices = ({ data, path }) => {
1616

1717
return {
1818
description: ware.description === '' ? ware.snippet : ware.description,
19+
snippet: ware.snippet,
1920
id: ware.id,
2021
img,
2122
name: ware.name,

utils/helpers.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,10 @@ export const formatBytes = (bytes, decimals = 2) => {
8282

8383
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
8484
}
85+
86+
export const truncateDescription = (desc = '', maxLength, isOpen) => {
87+
if (desc.length <= maxLength || isOpen) return { truncated: desc, cutOffIndex: desc.length }
88+
const lastSpaceIndex = desc.substring(0, maxLength).lastIndexOf(' ')
89+
const ellipsis = isOpen ? '' : '...'
90+
return { truncated: desc.slice(0, lastSpaceIndex) + ellipsis, cutOffIndex: lastSpaceIndex }
91+
}

0 commit comments

Comments
 (0)