Comments (4)
Ok, thanks, I appreciate you taking the time to clarify this for me - I just wanted to make sure I hadn't missed anything obvious (as I'm clearly coming in to contact with HTML structured tree data a lot later than you guys).
from contentful-cli.
Hey @openbook,
We're currently working on a markdown-to-rich-text app, also. Until that's ready I can share some code snippets that demonstrate how we use remark-parse
that might be useful to you.
Basically all the content for the rich text field is in the children
of the result of processor.parse()
. You have to iterate through each child and manually construct the rich text nodes according to the rich text schema.
Unfortunately a comprehensive rich text schema isn't quite public (beta feature :) ). From our community slack yesterday, the rich text PM wrote:
this is information that we have in some private repos but we are discussing on what is the best way to offer them to the public (documentation, or in the NPM readmes).
For now, to help you get moving, the list in the https://www.contentful.com/developers/docs/concepts/rich-text/ page is almost accurate: Add
EMBDEDDED_ASSET
to the list of blocks and you’re done
The other thing I can suggest is to create a rich text field manually in the Contentful web app, query for it using our APIs/SDKs, and examine the result, the way you've already done.
Here's a snippet from our forthcoming markdown-to-rich-text project (will be made public soon):
'use strict';
var unified = require('unified')
var markdown = require('remark-parse')
[...]
const nodeToSlate = (node) => {
const nodeType = nodeTypeFor(node)
const nodeContent = nodesToSlate(node.children)
let nodeValue = node.value
const nodeData = dataFor(node)
if (nodeType === 'text') {
let marks = []
if (node.type !== 'text') {
nodeValue = node.content ? node.content[0].value : node.value
marks.push({
type: markTypeFor(node),
object: 'mark',
data: {}
})
}
return {
nodeType: nodeType,
value: nodeValue,
marks: marks
}
} else {
return {
nodeType: nodeType,
content: nodeContent,
data: nodeData
}
}
}
const nodesToSlate = (nodes) => {
if (!nodes) return []
return nodes.map((node) => {
return nodeToSlate(node)
}).filter(Boolean)
}
const treeToSlate = (tree) => {
return {
nodeType: 'document',
content: nodesToSlate(tree.children)
}
}
module.exports = function (md) {
const processor = unified().use(markdown, { commonmark: true })
const tree = processor.parse(md)
return {
tree,
slate: treeToSlate(tree)
};
}
This is slightly out of date, but hopefully it demonstrates what I described above. We first create the 'document' node (in treeToSlate
function), then for each child we determine its node type and create the according rich text node, adding to our tree (in nodeToSlate
function).
from contentful-cli.
Thanks for your response, the technical solution makes sense. But, just so I understand this a bit better, am I correct in saying you've created, or are in the process of creating, a custom rich text schema for Contentful or is this an existing format? If it is custom, what prompted that choice rather than using HAST?
from contentful-cli.
Hello @openbook ,
I am the aforementioned PM and, while I would like to offer you a list of reasons why we didn't implement HAST, the plain truth is that our technical research on web editors and their underlying schema missed this AST proposal. Which is a pity, because this looks very close to what we have created (I haven't evaluated if custom node types such as Contentful Entries would be implementable with this yet, so I'm only judging by a first look) and I also see some familiar to Contentful names, such as K.Mathews, in the contributors list.
If it offers any proximal explanation to our decision, our research was revolved around popular Rich Text Editors, such as ProseMirror (we based our Markdown editor on CodeMirror), SlateJS, MobileDoc, QuillJS, DraftJS–for the record we landed on SlateJS on existing architectural compatibility grounds but the Contentful-based schema should allow us to switch the Editor in the future.
Moving forward, as we already have people using this editor and a schema change would be a breaking change, I can't promise that we will change our schema to HAST but we might consider offering a converter to these standards.
I hope I answered your question as well I could do and I'd be happy to provide more information if needed.
from contentful-cli.
Related Issues (20)
- `mkdirp is not a function` error when running `contentful space seed` HOT 3
- `merge show` and `merge export` commands result in `PollTimeout` error HOT 3
- Exit code 0 after failure with `--await-processing`
- Exits with status code 0 when failing in `--await-processing`
- Space export of published content HOT 1
- Embargoed PDF (4MB) asset cannot be imported to new contentful environment HOT 2
- Make it possible to skip more information when exporting a space
- `contentful space import` is stuck HOT 4
- Importing a locale with fallbackCode: null -> fallback gets set to the default locale
- Generated migration files swap field controls based on alpha order and field definition order HOT 3
- Plain text/JSON output for commands
- Please do less automatic releases
- Cloning an environment should also clone Workflows App configuration
- patch-package: not found HOT 2
- Error: Cannot find module esprima HOT 8
- Format variables in merge export --output-file
- `CRITICAL` Security Vulnerability
- Configured host is ignored for migration HOT 1
- Generated migration scripts does not contain the necessary parameters for `Contentful:GraphQLFieldResolver` annotation id
- renaming an environment
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from contentful-cli.