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

Org Roam V2 Changes #6

Closed
theianjones opened this issue Apr 29, 2021 · 3 comments
Closed

Org Roam V2 Changes #6

theianjones opened this issue Apr 29, 2021 · 3 comments

Comments

@theianjones
Copy link

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.

@rasendubi
Copy link
Owner

We should definitely have an example and I'd love to consider adding id links to the uniorg itself.

I have implemented limited support of id links in my own braindump before (rasendubi/braindump@41e2428). It only links to files though. Linking to headers requires stable anchors generation (similar to rehype-slug).

Having id link support in uniorg itself would require cross-file processing (currently, uniorg is a single-file parser). We could create a new uniorg package to do multi-file processing and extract common parts from examples.

@rasendubi
Copy link
Owner

@theianjones would you like to contribute the example?

@rasendubi
Copy link
Owner

Update: id links are now supported in uniorg

  • Implemented uniorg-slug to assign unique anchors for all headings (respecting CUSTOM_ID if present)
  • Implemented orgast-util-visit-ids, a utility to visit all nodes with ids
  • Combining these two makes it easy to implement id links for any user
  • Modified org-braindump template to support id links (68e6504)

cc @jethrokuan you were interested in id links support—uniorg now has it 🎉

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

2 participants