Skip to content

Instantly share code, notes, and snippets.

@kmelve
Last active June 7, 2019 08:49
Show Gist options
  • Select an option

  • Save kmelve/97ef2b073fa6296090d545582d0bbabc to your computer and use it in GitHub Desktop.

Select an option

Save kmelve/97ef2b073fa6296090d545582d0bbabc to your computer and use it in GitHub Desktop.
Character limit Portable Text (only top level)
{
"_createdAt": "2019-05-27T07:27:48Z",
"_id": "06b92a73-8502-45e8-ab8e-31b76181cf94",
"_rev": "CCW6uuhzMbOOH3oXLDotsE",
"_type": "post",
"_updatedAt": "2019-06-01T08:33:03Z",
"authors": [
{
"_key": "02bcfae524e0",
"_ref": "14165126-a0fd-4e5e-96af-686d36f408e0",
"_type": "reference"
}
],
"blurb": [
{
"_key": "9a2c8f9ec366",
"_type": "block",
"children": [
{
"_key": "9a2c8f9ec3660",
"_type": "span",
"marks": [],
"text": "No new features. No detailed plan. Just bug fixes for a week."
}
],
"markDefs": [],
"style": "normal"
}
],
"poster": {
"_type": "image",
"asset": {
"_ref": "image-4760c7501ecaa8f4a2618c314300402244493608-740x583-png",
"_type": "reference"
}
},
"publishedAt": "2019-05-30T22:00:00.000Z",
"shortTitle": "We had ourselves a bug week",
"slug": {
"_type": "slug",
"current": "we-had-ourselves-a-bug-week"
},
"text": [
{
"_key": "45ea897b4410",
"_type": "block",
"children": [
{
"_key": "45ea897b44100",
"_type": "span",
"marks": [],
"text": "If you have ever built or used software you know this: There will be bugs. When "
},
{
"_key": "45ea897b44101",
"_type": "span",
"marks": [
"7fe725c16d52"
],
"text": "Grace Hopper coined the phrase in software development"
},
{
"_key": "45ea897b44102",
"_type": "span",
"marks": [],
"text": " it proved to be an actual moth that had crept into the hardware and made trouble (original bug report in the photo above). As far as we know though, there aren't any moths inside our React code."
}
],
"markDefs": [
{
"_key": "7fe725c16d52",
"_type": "link",
"href": "https://en.wikipedia.org/wiki/Grace_Hopper#Anecdotes"
}
],
"style": "normal"
},
{
"_key": "dcc0395756f3",
"_type": "block",
"children": [
{
"_key": "dcc0395756f30",
"_type": "span",
"marks": [],
"text": "At Sanity.io we're a team of developers who make a platform for structured content, used by many different people for many different things. Feature requests from the community, strategic product decisions, and the needs of our customers will mostly end up getting prioritized. As it well should be. But it also means that it's easy to let those bugs rest in their hiding places while we focus on the new shiny stuff."
}
],
"markDefs": [],
"style": "normal"
},
{
"_key": "f82ec8869390",
"_type": "block",
"children": [
{
"_key": "f82ec88693900",
"_type": "span",
"marks": [],
"text": "But bugs aren't just annoying for people who may encounter them when using our software, they're also… well, bugging us who develop and design the product. Because we know they are there. Some bugs may never really affect users directly or just in some rare cases, while some bugs are more intrusive. Either way, there's nothing more we would like, than to squash them."
}
],
"markDefs": [],
"style": "normal"
},
{
"_key": "76f0f6d2dd76",
"_type": "block",
"children": [
{
"_key": "76f0f6d2dd760",
"_type": "span",
"marks": [],
"text": "So we had ourselves a bug week!"
}
],
"markDefs": [],
"style": "normal"
},
{
"_key": "527d586b77a0",
"_type": "block",
"children": [
{
"_key": "527d586b77a00",
"_type": "span",
"marks": [],
"text": "No new features. No detailed sprint plan. Every developer got to pick the bugs that annoyed them the most and fix it. The result: "
},
{
"_key": "527d586b77a01",
"_type": "span",
"marks": [
"cfd148eccfed"
],
"text": "32 Pull Requests to the next-branch"
},
{
"_key": "527d586b77a02",
"_type": "span",
"marks": [],
"text": ". "
}
],
"markDefs": [
{
"_key": "cfd148eccfed",
"_type": "link",
"href": "https://github.com/sanity-io/sanity/pulls?utf8=%E2%9C%93&q=is%3Apr+sort%3Aupdated-desc+created%3A2019-05-19..2019-05-27"
}
],
"style": "normal"
},
{
"_key": "a860fa65ad7b",
"_type": "block",
"children": [
{
"_key": "a860fa65ad7b0",
"_type": "span",
"marks": [],
"text": "We have fixed bugs in the CLI, Structure Builder, error handling, the editor for Portable Text, the Form Builder, the mutator, visual components, block-tools, and validation. Pull Requests will now be reviewed one-by-one and shipped to you in the next releases. "
}
],
"markDefs": [],
"style": "normal"
},
{
"_key": "79aeef96cb1c",
"_type": "block",
"children": [
{
"_key": "79aeef96cb1c0",
"_type": "span",
"marks": [],
"text": "It felt invigorating! Cathartic even! And we're definitely doing bug week again! "
}
],
"markDefs": [],
"style": "normal"
},
{
"_key": "d71e726e8fa5",
"_type": "callToAction",
"linkText": "Check out the release notes on GitHub",
"url": "https://github.com/sanity-io/sanity/releases/tag/v0.140.30"
}
],
"title": "We had ourselves a bug week"
}
const doc = require('./doc.json')
const defaults = {nonTextBehavior: 'remove', limit: 250}
function textLimit (doc, opts = {}) {
const { limit = 160, ellipsis = '...' } = opts
let count = 0
let limitReached = false
const excerpt = doc.reduce((blocks, block) => {
if(limitReached) {
return blocks
}
if (block._type !== 'block') {
return [...blocks, block]
}
const newBlock = { ...block, children: block.children.filter(Boolean).map(child => {
const spanLength = child.text.length
if ((count + spanLength) < limit) {
count = count + spanLength
return child
}
const newSpan = {...child, text: child.text.slice(0, limit - count) + ellipsis}
limitReached = true
return newSpan
})}
return [...blocks, newBlock]
}, [])
return excerpt
}
const excerpt = textLimit(doc.text, {limit: 250})
console.log(blocksToText(excerpt))
function blocksToText(blocks, opts = {}) {
const options = Object.assign({}, defaults, opts)
return blocks
.map(block => {
if (block._type !== 'block' || !block.children) {
return options.nonTextBehavior === 'remove' ? '' : `[${block._type} block]`
}
return block.children.map(child => child.text).join('')
})
.join('\n\n')
}
@kmelve
Copy link
Copy Markdown
Author

kmelve commented Jun 5, 2019

This outputs

If you have ever built or used software you know this: There will be bugs. When Grace Hopper coined the phrase in software development it proved to be an actual moth that had crept into the hardware and made trouble (original bug report in the photo ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment