Skip to content

Instantly share code, notes, and snippets.

@d4rekanguok
Last active February 23, 2024 20:35
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save d4rekanguok/8a6c698d16ef6666196ae028c04066bc to your computer and use it in GitHub Desktop.
Save d4rekanguok/8a6c698d16ef6666196ae028c04066bc to your computer and use it in GitHub Desktop.
const { default: Schema } = require("@sanity/schema");
const { htmlToBlocks, randomKey } = require("@sanity/block-tools");
const { JSDOM } = require("jsdom");
const schemaBlog = Schema.compile({
name: "myBlog",
types: [
{
title: "Hjelpeartikkel",
name: "hjelpeartikkel",
type: "document",
fields: [
{
title: "Title",
name: "title",
type: "string",
},
],
},
{
title: "Content",
name: "content",
type: "array",
of: [
{
type: "block",
marks: {
annotations: [
{
name: "hjelpeartikkelLink",
type: "object",
title: "Lenke til en hjelpeartikkel",
fields: [
{
name: "reference",
type: "reference",
to: [{ type: "hjelpeartikkel" }],
},
],
},
],
},
},
],
},
],
});
const hjelpeartikkelLink = (num, children) => ({
_type: "__annotation",
markDef: {
_type: "hjelpeartikkelLink",
_key: randomKey(12),
reference: {
_type: "reference",
_ref: `hjelpeartikkel-${num}`,
},
},
children,
});
const blockContentType = schemaBlog.get("content");
const blocks = htmlToBlocks(
/* html */
`
<html>
<body>
<li data-identifyelement="486">
Er utgiften relevant for foretaket ditt? Det vil si: hører kostnaden hjemme i regnskapet ditt i det hele tatt? Se for eksempel vår hjelpeartikkel om
<a
data-identifyelement="480"
href="https://hjelp.fiken.no/support/solutions/articles/13000035066-vanlige-fradrag-i-enkeltpersonforetak"
rel="noreferrer" target="_blank">
vanlige fradrag i enkeltpersonforetak
</a>
hvis du vil vite mer om dette.
</li>
<body>
</html>
`,
blockContentType,
{
parseHtml: (html) => new JSDOM(html).window.document,
rules: [
{
deserialize(el, next) {
if (
el.tagName === "A" &&
el.href?.match(
/hjelp.fiken.no\/support\/solutions\/articles\/[0-9]+/
)
) {
const num = el.href.match(/[0-9]+/)[0];
return hjelpeartikkelLink(num, next(el.childNodes));
}
},
},
],
}
);
console.dir(blocks, { depth: null });
// [
// {
// _type: "block",
// markDefs: [
// {
// _type: "hjelpeartikkelLink",
// _key: "4781f2e2fb04",
// reference: { _type: "reference", _ref: "hjelpeartikkel-13000035066" },
// },
// ],
// style: "normal",
// children: [
// {
// _type: "span",
// marks: [],
// text: "Er utgiften relevant for foretaket ditt? Det vil si: hører kostnaden hjemme i regnskapet ditt i det hele tatt? Se for eksempel vår hjelpeartikkel om",
// _key: "63657b14e1b10",
// },
// {
// _type: "span",
// marks: ["4781f2e2fb04"],
// text: " vanlige fradrag i enkeltpersonforetak",
// _key: "63657b14e1b11",
// },
// {
// _type: "span",
// marks: [],
// text: " hvis du vil vite mer om dette.",
// _key: "63657b14e1b12",
// },
// ],
// _key: "63657b14e1b1",
// },
// ];
const { default: Schema } = require('@sanity/schema')
const { htmlToBlocks, randomKey } = require('@sanity/block-tools')
const { JSDOM } = require('jsdom')
const schemaBlog = Schema.compile({
name: 'myBlog',
types: [
{
title: 'Content',
name: 'content',
type: 'array',
of: [
{
type: 'block',
marks: {
annotations: [
{
name: 'externalLink',
type: 'object',
title: 'External Link',
fields: [
{
name: 'href',
type: 'url',
title: 'URL',
},
]
}
]
}
}
]
}
]
})
const blockContentType = schemaBlog.get('content')
const blocks = htmlToBlocks(/* html */
`
<html>
<body>
<h1>Hello world!</h1>
<a href="/hello-world">Hello World</a>
<body>
</html>
`,
blockContentType,
{
parseHtml: html => new JSDOM(html).window.document,
rules: [
{
deserialize(el, next, block) {
if (el.tagName !== 'A') return
return {
_type: '__annotation',
markDef: {
// extract properties from <a /> and
// populate your `externalLink` object here
_type: 'externalLink',
_key: randomKey(12),
href: el.href,
},
children: next(el.childNodes)
}
}
}
]
}
)
console.dir({ blocks }, { depth: null })
// expected outcome:
// {
// blocks: [
// {
// _type: 'block',
// markDefs: [],
// style: 'h1',
// children: [
// {
// _type: 'span',
// marks: [],
// text: 'Hello world!',
// _key: 'b3a4521595840'
// }
// ],
// _key: 'b3a452159584'
// },
// {
// _type: 'block',
// markDefs: [
// {
// _type: 'externalLink',
// _key: 'f41a3276f220',
// href: '/hello-world'
// }
// ],
// style: 'normal',
// children: [
// {
// _type: 'span',
// marks: [ 'f41a3276f220' ],
// text: 'Hello World',
// _key: '35569e4ee1a60'
// }
// ],
// _key: '35569e4ee1a6'
// }
// ]
// }
@missiontide
Copy link

THANK YOU! This was what I needed to get links working in html > portableText

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