spencermountain / wtf_wikipedia

a pretty-committed wikipedia markup parser
https://observablehq.com/@spencermountain/wtf_wikipedia
MIT License
770 stars 129 forks source link

Unsupported Template example confusing #533

Closed tmtmtmtm closed 1 year ago

tmtmtmtm commented 1 year ago

The Adding new templates section of the docs provide sample code that I can use to handle unknown templates.

However, I can't work out how to actually get that to work. My reading of the docs suggest I should be able to do something like:

const wtf=require('wtf_wikipedia')

wtf.extend((models, templates) => {
  templates.unknown1 = 'handled'; 
})

wtf(wiki, {
  templateFallbackFn: (tmpl, list, parse) => {
    let obj = parse(tmpl)
    list.push(obj)
    return '[unsupported template]'
  }
})

let str = ` {{Infobox officeholder
  | name                = Dr. Rajesh Sonkar
  | office1             = {{unknown1}}
  | office2             = {{unknown2}}
  }}`
let doc = wtf(str)
doc.infobox().json()

But when I run that, it complains that wiki is not defined, and I can't see elsewhere what that should be, or where this should live instead.

spencermountain commented 1 year ago

oh yeah, looks like a bug in the docs wtf(wiki, { should probably be wtf.extend({ the rest of that example looks good to me. will fix the readme this week. cheers

spencermountain commented 1 year ago

fixed the readme example thanks

tmtmtmtm commented 1 year ago

@spencermountain I'm afraid I'm even more confused now! I thought this section was about what should happen when the parser encounters any unknown template: i.e. in the example at https://runkit.com/tmtmtmtm/6458a3a86332d60008a93ee1 office1 would become 'handled', and both office2 and office3 would become '[unsupported template]'.

Have I completely misunderstood what this is about? Or what should I change in the notebook code to make that happen?

spencermountain commented 1 year ago

here's an update that works https://runkit.com/spencermountain/645ead27cc10ea0008a3b575 cheers

tmtmtmtm commented 1 year ago

@spencermountain Aha! Thanks — that's very helpful and sent me in the right direction. I had been thinking this was something that would be configured in advance, like the wtf.extend example.

It might be worth giving an example here of how to use this with wtf.fetch as I needed to do a bit more spelunking to work how to do actually do this in that scenario. I ended up having to change:

wtf.fetch(pages, 'en', function(err, docList) {

to

wtf.fetch(pages, {
  lang: 'en',
  templateFallbackFn: (tmpl, list, parse) => {
    let obj = parse(tmpl)
    list.push(obj)
    return `[unsupported template: ${tmpl}]`
  }
}, function(err, docList) {

This feels a little clunky, so I'm not sure if there's a better approach.