You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Nice that you also made an HTML extension! Fwiw, it isn’t used by remark/unified/etc, so it is a bit of duplicate work. However, I do think some folks will benefit from using micromark to go to HTML directly instead of complex AST stuff, so having extensions like this available for micromark directly is cool!
functionhtml(opts={}){// ! I think `permalinks` should exist on the state, it would leak I think// with repeat use:// ```// var html = wikiLinkHtml({})// // micromark({htmlExtensions: [html]})// micromark({htmlExtensions: [html]})// ```constpermalinks=opts.permalinks||[]constdefaultPageResolver=(name)=>[name.replace(//g,'_').toLowerCase()]constpageResolver=opts.pageResolver||defaultPageResolverconstnewClassName=opts.newClassName||'new'constwikiLinkClassName=opts.wikiLinkClassName||'internal'constdefaultHrefTemplate=(permalink)=>`#/page/${permalink}`consthrefTemplate=opts.hrefTemplate||defaultHrefTemplatefunctionenterWikiLink(){// ! If you’re not having links in links, you don’t need a stack.// What about:// ```// this.setData('wikiLinkAlias', '')// this.setData('wikiLinkTarget', '')// ```// instead of the rest:letstack=this.getData('wikiLinkStack')if(!stack)this.setData('wikiLinkStack',(stack=[]))stack.push({})}functiontop(stack){returnstack[stack.length-1]}functionexitWikiLinkAlias(token){// ! And// ```// this.setData('wikiLinkAlias', this.sliceSerialize(token))// ```// instead of the rest://// Note btw: if you have character escapes and character references (with a// “string”), you should start `this.buffer()` in a new `enterWikiLinkAlias`,// and use `this.resume()` to get the data here.constalias=this.sliceSerialize(token)constcurrent=top(this.getData('wikiLinkStack'))current.alias=alias}functionexitWikiLinkTarget(token){// ! And// ```// this.setData('wikiLinkTarget', this.sliceSerialize(token))// ```// instead of the rest:// (same as note on escapes as previous)consttarget=this.sliceSerialize(token)constcurrent=top(this.getData('wikiLinkStack'))current.target=target}functionexitWikiLink(){// ! And// ```// let target = this.getData('wikiLinkTarget')// let displayName = this.getData('wikiLinkAlias') || target// ... at end of function to clear ’em:// this.setData('wikiLinkAlias')// this.setData('wikiLinkTarget')// ```constwikiLink=this.getData('wikiLinkStack').pop()constpagePermalinks=pageResolver(wikiLink.target)// I don’t think `permalinks` is added to yet? Should that happen?letpermalink=pagePermalinks.find(p=>permalinks.indexOf(p)!==-1)constexists=permalink!==undefinedif(!exists){permalink=pagePermalinks[0]}letdisplayName=wikiLink.targetif(wikiLink.alias){displayName=wikiLink.alias}letclassNames=wikiLinkClassNameif(!exists){classNames+=' '+newClassName}// ! Should `hrefTemplate` be made safe?// If needed this can be externalised:// <https://github.com/micromark/micromark/blob/63cf51445077f18145d35dd3c506859d6f8195ce/lib/compile/html.js#L385>this.tag(// href should be encoded (and potentially normalized):// <https://github.com/micromark/micromark/blob/63cf51445077f18145d35dd3c506859d6f8195ce/lib/util/normalize-uri.js#L10>'<a href="'+hrefTemplate(permalink)+// ! `this.encode(classNames)`'" class="'+classNames+'">')// ! Use `this.raw(this.encode(displayName))` to make it safe (to encode < and & and so).this.raw(displayName)this.tag('</a>')}return{enter: {wikiLink: enterWikiLink},exit: {wikiLinkTarget: exitWikiLinkTarget,wikiLinkAlias: exitWikiLinkAlias,wikiLink: exitWikiLink}}}export{html}
The text was updated successfully, but these errors were encountered:
Nice that you also made an HTML extension! Fwiw, it isn’t used by remark/unified/etc, so it is a bit of duplicate work. However, I do think some folks will benefit from using micromark to go to HTML directly instead of complex AST stuff, so having extensions like this available for micromark directly is cool!
The text was updated successfully, but these errors were encountered: