prismicio / javascript-kit

Development kit for the Javascript language
https://developers.prismic.io
106 stars 69 forks source link

Link resolver params #119

Closed azappa closed 7 years ago

azappa commented 8 years ago

I made a prismic repo with blog posts in two languages (EN, IT) so i duplicate fields like

"IT" : {
  "title-IT" : {
    "type" : "StructuredText",
    "config" : {
      "single" : "heading1",
    }
  }
},
"EN" : {
  "title-EN" : {
    "type" : "StructuredText",
    "config" : {
      "single" : "heading1",
    }
  }
},

and using i18n-2 for routing passing a variable as language parameter. In my pug templates for posts how can i redirect linked posts to correct lang? At the moment I have something like this:

div.block-text!=slice.value.asHtml(ctx.linkResolver)

My prismic.configuration.js got this fn as linkResolver

  linkResolver: (doc) => {
    if (doc.isBroken) { return false; }
    if (doc.type === 'articolo') { return `/${doc.tags[0]}/${doc.uid}/${doc.id}`; }

    return `/${doc.slug}/${doc.id}`;
  },

I know I can access to doc and ctx as parameters but I cannot pass other ones. Is there a way in route get fn where I can set into ctx a variable so I can use that value in my linkResolver fn?

azappa commented 8 years ago

Got working, it not so a clean method but I can't do in other way...

In my pug template:

div.block-text!=slice.value.asHtml(ctx.linkResolver, (e, c) => { if (e.type === 'hyperlink') { return `<a href="/${(currentLang || defaultLang)}${e.url}">${c}</a>`; } return null; })

'Cleaner' methods are welcome!

arnaudlewis commented 7 years ago

@azappa You could simply have configured an HtmlSerializer somewhere and just put it in your prismicContext just like in this project : https://github.com/levimykel/html-serializer-example/blob/master/prismic-configuration.js#L22