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

Validate resources #408

Merged
merged 20 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 28 additions & 29 deletions .github/scripts/getResourcesOg.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const ogs = require('open-graph-scraper')
const fs = require('fs')
const path = require('path')

const resourcesPath = './components/ResourceHub/company-resources.json'
const communityResourcesPath =
Expand All @@ -19,37 +18,37 @@ const updateResourceOg = async () => {
)

const updatedResources = await Promise.all(
resources
.filter(r => r.type !== 'video')
.map(async resource => {
const og = await getResourceOg(resource.url)
return {
...resource,
og: {
title: og.ogTitle,
description: og.ogDescription,
image: og.ogImage[0]?.url
}
}
})
resources.map(async resource => {
const og = await getResourceOg(resource.url)
const description = resource.description ?? og.ogDescription
return {
name: resource.name ?? og.ogTitle,
url: resource.url,
type: resource.type,
date: resource.date,
description: description.length > 197 ? description.slice(0, 197) + '...' : description,
tags: resource.tags,
image: og.ogImage[0]?.url
}
})
)

const updatedCommunityResources = await Promise.all(
communityResources
.filter(r => r.type !== 'video')
.map(async resource => {
const og = await getResourceOg(resource.url)
return {
...resource,
og: {
title: og.ogTitle,
description: og.ogDescription,
...(!(parseInt(og.ogImage[0].height) < 60) && {
image: og.ogImage[0].url
})
}
}
})
communityResources.map(async resource => {
const og = await getResourceOg(resource.url)
const description = resource.description ?? og.ogDescription
return {
name: resource.name ?? og.ogTitle,
url: resource.url,
type: resource.type,
date: resource.date,
description: description.length > 197 ? description.slice(0, 197) + '...' : description,
tags: resource.tags,
...(!(parseInt(og.ogImage[0].height) < 60) && {
image: og.ogImage[0].url
})
}
})
)

fs.writeFileSync(resourcesPath, JSON.stringify(updatedResources, null, 2))
Expand Down
58 changes: 58 additions & 0 deletions .github/scripts/validateResources.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require('fs')

const resourcesPath = './components/ResourceHub/company-resources.json'
const communityResourcesPath =
'./components/ResourceHub/community-resources.json'

const validateResource = (resource, index, resources) => {
if (resources.findIndex(r => r.url === resource.url) !== index) {
console.log(`Duplicate resource`)
return false
}
if (!resource.name) {
console.log(`Resource name is missing`)
return false
}
if (!resource.type) {
console.log(`Resource type is missing`)
return false
}
if (!resource.date) {
console.log(`Resource date is missing`)
return false
}
if (!resource.description) {
console.log(`Resource description is missing`)
return false
}
if (resource.description.length > 200) {
console.log(`Resource description is too long`)
return false
}
if (!resource.tags || !resource.tags.length) {
console.log(errorMessage + `Resource tags are missing`)
return false
}
if (
resource.url.includes('youtube') &&
(!resource.url.startsWith('https://www.youtube.com/watch?v=') ||
resource.url.length !== 43)
) {
console.log(`Invalid YouTube URL`)
return false
}
return true
}

const validateResources = () => {
const resources = [
...JSON.parse(fs.readFileSync(resourcesPath, 'utf8')),
...JSON.parse(fs.readFileSync(communityResourcesPath, 'utf8'))
]

if (resources.map(validateResource).some(valid => !valid)) {
process.exit(1)
}
}

validateResources()
4 changes: 2 additions & 2 deletions .github/workflows/generate-supported-networks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Use Node.js 20.x
uses: actions/setup-node@v2
uses: actions/setup-node@v4
with:
node-version: '20.x'
- uses: pnpm/action-setup@v2
- uses: pnpm/action-setup@v3
with:
version: 8
- name: Install dependencies
Expand Down
17 changes: 17 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ We use the [Microsoft Style Guide](https://learn.microsoft.com/en-us/style-guide

Please open an [issue](https://github.com/safe-global/safe-docs/issues/new?assignees=&labels=resource-hub&projects=&template=resource-hub-submission.yml&title=%5BResource+Hub%5D+) to submit a resource to our Resource Hub.

### Adding Resources to Resource Hub

[For the Safe team] To add a new resource to the Resource Hub, follow these steps:
- Add the new entry (`type`, `url`, `tags`, and `date`) to the [community-resources.json](./components/ResourceHub/community-resources.json) or [company-resources.json](./components/ResourceHub/company-resources.json) file.
```
{
"url": "https://example.com",
"type": "Blog Post",
"date": "2023-10-09",
"tags": [
"Tutorial"
]
}
```
- Run `pnpm get-resources-og` to fetch open graph metadata for the new resource automatically.
- Copy edit the automatically generated copy for `name` and `description`.

## Using Vale

Install the [Vale CLI](https://vale.sh/docs/vale-cli/installation/) and run the following command:
Expand Down
6 changes: 3 additions & 3 deletions components/ResourceHub/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export const ProjectCard = (resource: KnowledgeResource): JSX.Element => {
<div style={{ width: '100%' }}>
{resource.type === 'Video' ? (
<YouTubeEmbed embedId={resource.url.slice(-11)} />
) : resource?.og?.image == null ? (
) : resource?.image == null ? (
resource.type === 'Blog Post' ? (
<Blog />
) : (
<Podcast />
)
) : (
<Box minHeight='100px'>
<img alt='resource-img' src={resource.og.image} />
<img alt='resource-img' src={resource.image} />
</Box>
)}

Expand All @@ -80,7 +80,7 @@ export const ProjectCard = (resource: KnowledgeResource): JSX.Element => {
color='text.secondary'
className={css.description}
>
{resource.abstract}
{resource.description}
</Typography>
</>
)}
Expand Down
8 changes: 2 additions & 6 deletions components/ResourceHub/Resources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ export interface KnowledgeResource {
type: string
date: string
origin: string
abstract?: string
description: string
tags: string[]
og?: {
title?: string
description?: string
image?: string
}
image?: string
}

const getUniqueStrings = (entries: string[]): string[] => {
Expand Down
Loading
Loading