rasendubi / uniorg

An accurate Org-mode parser for JavaScript/TypeScript
https://oleksii.shmalko.com/uniorg
GNU General Public License v3.0
263 stars 25 forks source link

Processing attachments #18

Closed elsehow closed 3 years ago

elsehow commented 3 years ago

Thank you for this package!

When I write, I often include images with attachments. I am trying to use this library to convert <img src="attachment:my-image.jpg"> to <img src="my-image.jpg">.

Does the library provide this functionality?

rasendubi commented 3 years ago

Do you mean using org-attach and [[attachment:…]] links? Uniorg does not offer this right now (or any link processing). However, it is possible to process URLs with rehype-url-inspector.

e.g., resolveLinks in org-braindump example resolves id: and file: links: https://github.com/rasendubi/uniorg/blob/c1ee8de6e54693bf30dd95d065748d93ddf376e6/examples/org-braindump/src/lib/resolveLinks.js#L4-L63

To implement this in a general way, I would visit every link and if it’s an attachment, traverse up to a headline and see if it has DIR or ID property, then resolve the attachment path accordingly. unist-util-visit-parents might help here because it provides a list of ancestors.

elsehow commented 3 years ago

Do you mean using org-attach and [[attachment:…]] links?

Exactly!

I tried the following, just to try to get a sense of what was going on:

  if (url.protocol === 'attachment:') {
    node.properties[propertyName] = '';
  }

But to no avail. I still see HTML links like src="attachment:undersea-cable-map.jpeg"

I think my familiarity with the unified ecosystem isn't quite where it needs to be to implement what you describe, but it's exactly what I'm looking for. If you or someone else can implement this, I think it would be an extremely useful feature/workflow!

rasendubi commented 3 years ago

Where did your plug that code?

On Wed, Aug 11, 2021, 02:19 Nick Merrill @.***> wrote:

Do you mean using org-attach and [[attachment:…]] links?

Exactly!

I tried the following, just to try to get a sense of what was going on:

if (url.protocol === 'attachment:') {

node.properties[propertyName] = '';

}

But to no avail. I still see HTML links like src="attachment:undersea-cable-map.jpeg"

I think my familiarity with the unified ecosystem isn't quite where it needs to be to implement what you describe, but it's exactly what I'm looking for. If you or someone else can implement this, I think it would be an extremely useful feature/workflow!

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/rasendubi/uniorg/issues/18#issuecomment-896374931, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAKNTE2QZ7JAZP3DV5L7UHTT4GXXVANCNFSM5B24DGDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

elsehow commented 3 years ago

I put it in processUrl: https://github.com/elsehow/else.how/blob/master/src/lib/processUrls.ts#L77-L79

rasendubi commented 3 years ago

Strange… it should have worked.

Anyway, I’ll try to implement a plugin to transform attachment links to file links when I have more spare time (hopefully, September). If you (or anyone else) want to implement this earlier, I can provide guidance.

rasendubi commented 3 years ago

Quick question. Do you use org-attach-id-dir?

elsehow commented 3 years ago

The variable is set (to .attach in my notes directory).

On Mon, Aug 30, 2021 at 6:17 AM Alexey Shmalko @.***> wrote:

Quick question. Do you use org-attach-id-dir?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/rasendubi/uniorg/issues/18#issuecomment-908334850, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABWNKLLMTLZMKUHUGCNRNSDT7OAFLANCNFSM5B24DGDA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

rasendubi commented 3 years ago

I have just published uniorg-attach package to convert attachment: links to file: links.

It would be great if you could give it a try. Your code should look something like that:

...
import { uniorgAttach } from 'uniorg-attach';
...

unified()
  .use(uniorgParse)
  .use(uniorgAttach, { idDir: '.attach' })
  .use(uniorg2rehype)

(Note that uniorgAttach must be used before uniorg2rehype.)