Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review: html #2

Open
wooorm opened this issue Oct 17, 2020 · 0 comments
Open

Review: html #2

wooorm opened this issue Oct 17, 2020 · 0 comments

Comments

@wooorm
Copy link

wooorm commented Oct 17, 2020

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!

function html (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]})
  // ```
  const permalinks = opts.permalinks || []
  const defaultPageResolver = (name) => [name.replace(/ /g, '_').toLowerCase()]
  const pageResolver = opts.pageResolver || defaultPageResolver
  const newClassName = opts.newClassName || 'new'
  const wikiLinkClassName = opts.wikiLinkClassName || 'internal'
  const defaultHrefTemplate = (permalink) => `#/page/${permalink}`
  const hrefTemplate = opts.hrefTemplate || defaultHrefTemplate

  function enterWikiLink () {
    // ! 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:
    let stack = this.getData('wikiLinkStack')
    if (!stack) this.setData('wikiLinkStack', (stack = []))

    stack.push({})
  }

  function top (stack) {
    return stack[stack.length - 1]
  }

  function exitWikiLinkAlias (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.
    const alias = this.sliceSerialize(token)
    const current = top(this.getData('wikiLinkStack'))
    current.alias = alias
  }

  function exitWikiLinkTarget (token) {
    // ! And
    // ```
    // this.setData('wikiLinkTarget', this.sliceSerialize(token))
    // ```
    // instead of the rest:
    // (same as note on escapes as previous)
    const target = this.sliceSerialize(token)
    const current = top(this.getData('wikiLinkStack'))
    current.target = target
  }

  function exitWikiLink () {
    // ! 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')
    // ```
    const wikiLink = this.getData('wikiLinkStack').pop()

    const pagePermalinks = pageResolver(wikiLink.target)
    // I don’t think `permalinks` is added to yet? Should that happen?
    let permalink = pagePermalinks.find(p => permalinks.indexOf(p) !== -1)
    const exists = permalink !== undefined
    if (!exists) {
      permalink = pagePermalinks[0]
    }
    let displayName = wikiLink.target
    if (wikiLink.alias) {
      displayName = wikiLink.alias
    }

    let classNames = wikiLinkClassName
    if (!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 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant