
Product
Socket Now Supports pylock.toml Files
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.
@gridsome/source-contentful
Advanced tools
Contentful source for Gridsome. This package is under development and API might change before v1 is released.
yarn add @gridsome/source-contentful
npm install @gridsome/source-contentful
module.exports = {
plugins: [
{
use: '@gridsome/source-contentful',
options: {
space: 'YOUR_SPACE', // required
accessToken: 'YOUR_ACCESS_TOKEN', // required
host: 'cdn.contentful.com',
environment: 'master',
typeName: 'Contentful'
}
}
]
}
To add custom routes use the templates
config with the collection type name as the key and the custom route as the value.
If you have Contentful ContentTypes named BlogPost and Article you can add new routes like this:
module.exports = {
templates: {
ContentfulBlogPost: '/blog/:slug',
ContentfulArticle: '/articles/:slug'
}
}
@gridsome/souce-contentful
currently works with all Contentful Content Types.
Contentful Rich text content types return a custom JSON response that can only be parsed to HTML with Contentful's package, https://www.npmjs.com/package/@contentful/rich-text-html-renderer
A query that returns Contentful Rich Text, where richArticle
is the Rich Text content type configured in the Contentful Content model:
query RichArticles {
allContentfulArticle {
edges {
node {
id
title
richArticle
}
}
}
}
Rich Text fields returns a JSON document which can be used with @contentful/rich-text-html-renderer
to generate HTML. The content from richArticle
can then be passed to a Vue method
from the page <template>
. In this case, the method name is richtextToHTML
:
<div v-for="edge in $page.articles.edges" :key="edge.node.id">
<p v-html="richtextToHTML(edge.node.richArticle)"></p>
</div>
Finally, the method to convert the JSON document into HTML (in the most basic usage):
import { documentToHtmlString } from '@contentful/rich-text-html-renderer'
export default {
methods: {
richtextToHTML (content) {
return documentToHtmlString(content)
}
}
}
The Contentful renderer is imported, then used to convert the JSON response from the page-query
.
Custom parsing and more configuration details can be found on the Contentful Rich Text HTML Render package documentation
The Contentful HTML renderer doesn't automatically render embedded assets, instead, you can configure how you want to render them using BLOCK
types and the configuration options.
To do so, import BLOCKS
and setup a custom renderer before calling the documentToHtmlString
method. Here, we're getting the image title and source url (contentful CDN src) and passing it to a string template.
import { BLOCKS } from '@contentful/rich-text-types'
import { documentToHtmlString } from '@contentful/rich-text-html-renderer'
export default {
methods: {
richTextToHTML (content) {
return documentToHtmlString(content, {
renderNode: {
[BLOCKS.EMBEDDED_ASSET]: (node) => {
return `<img src="${node.data.target.fields.file.url}" alt="${node.data.target.fields.title}" />`
}
}
})
}
}
}
Rich Text fields can take an html
argument to return generated HTML instead of a Rich Text document. The generated HTML can simply be passed in to an element with v-html
.
query Article($id: String!) {
contentfulArticle(id: $id) {
id
title
richArticle(html: true)
}
}
<div v-html="$page.contentfulArticle.richArticle" />
Contentful Location data is returned as JSON with lat
and lon
. You will need to query the field name and each field in the GraphQL query.
query Location {
allContentfulTestType {
edges {
node {
geoLocation {
lat
lon
}
}
}
}
}
In Contentful JSON ContentTypes, rather than receiving the entire object when querying for the field, GraphQL requires that you query for each field that you need.
query Json {
allContentfulTestType {
edges {
node {
jsonFieldName {
itemOne
itemTwo
}
}
}
}
}
FAQs
Contentful source for Gridsome
The npm package @gridsome/source-contentful receives a total of 0 weekly downloads. As such, @gridsome/source-contentful popularity was classified as not popular.
We found that @gridsome/source-contentful demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Socket now supports pylock.toml, enabling secure, reproducible Python builds with advanced scanning and full alignment with PEP 751's new standard.
Security News
Research
Socket uncovered two npm packages that register hidden HTTP endpoints to delete all files on command.
Research
Security News
Malicious Ruby gems typosquat Fastlane plugins to steal Telegram bot tokens, messages, and files, exploiting demand after Vietnam’s Telegram ban.