Skip to content

Org Roam V2 Changes #6

Closed
Closed
@theianjones

Description

@theianjones

First of all, thanks for making this! I love it.

I have recently upgraded to org roam v2 (its still in alpha as I write this heres the latest upgrade guide)

A big change is that org roam is moving to ids for everything. So each file gets a top level id in the property drawer. The other big change is that all the links get turned to ids. This makes it kind of challenging to parse those links and replace them with the route that you want because previously they were just file paths.

Heres how I am collecting all the ids:

(link to code)

function extractExportSettings() {
  return transformer

  function transformer(node: any, file: any) {
    visit(node, 'property-drawer', (prop: any, index, parent) => {
      // if the parents contentsBegin is 0 we know its the first one

      if (parent?.type === 'section' && parent.contentsBegin === 0) {
        prop.children.forEach((child: NodeProperty) => {
          const key = child.key.toLowerCase()
          file.data[key] = child.value
          if (key === 'id') {
            assign(fileIdToPath, {[child.value]: file.history[0]})
          }
        })
      }
    })
    // Visit every keyword in the org file and copy its value to the
    // file. file is then returned from processor.process, so all
    // keywords are available outside.
    visit(node, 'keyword', function (kw: any) {
      file.data[kw.key.toLowerCase()] = kw.value
    })
  }
}

this adds the id and the roam_ref that is often in the top level property drawer.

Im also adding the id and path to a global object so that i can change the link to a path in this code:

(link to code)

function processUrl({
  url: urlString,
  propertyName,
  node,
  file,
}: ProcessUrlArgs) {
  // next/link does not handle relative urls properly. Use file.path
  // (the slug of the file) to normalize link against.
  try {
    let url = new URL(urlString, 'file://' + file.path)


    if (url.protocol === 'file:') {
      let href = url.pathname.replace(/\.org$/, '')
      node.properties[propertyName] = href


      file.data.links = file.data.links ?? []
      file.data.links.push(href)
    } else if (url.protocol === 'id:') {
      const id = url.pathname
      let path = generateFileSlug(get(fileIdToPath, id))


      if (
        process.env.NODE_ENV === 'development' &&
        file.data.published &&
        typeof path === 'undefined'
      ) {
        // console.debug(`Unresolved link in ${file.data.slug}`)
      } else {
        let href = `/notes/${path}`
        node.properties[propertyName] = href
        file.data.links = file.data.links ?? []
        file.data.links.push(path)
      }
    }
  } catch (e) {
    // This can happen if org file contains an invalid string, that
    // looks like URL string (e.g., "http://example.com:port/" passes
    // regexes, but fails to parse as URL).
    console.warn('Failed to process URL', urlString, e)
    // No re-throwing: the issue is not critical enough to stop
    // processing. The document is still valid, it's just link that
    // isn't.
  }
}

Do you have any plans on updating your example or supporting org roam v2 within uniorg itself? I'd love to hear your thoughts on this!

Thanks again for your work.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions