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

Request: extend options to allow for custom anchor names (not just prefix) #166

Closed
fearthecowboy opened this issue May 6, 2020 · 4 comments

Comments

@fearthecowboy
Copy link

I realize that the anchor names themselves aren't significant in YAML, but I was wondering if you could squeeze in a feature in the options to permit a function to generate the anchor name for a given object instead of just allowing a prefix.

I'm working with rather large YAML files at the moment, and it would be immensely helpful if the anchor name could be configured to let me smuggle in some contextual information in the name of the anchor, so when I'm reviewing the files, I know what I'm looking at without having to go see the other side of the anchor.

something like :

let counter = 0;

stringify(data, { 
  customTags: [elementTag],
  customAnchor: (value) => { 
     // on my objects that have .internalname
     if( typeof value === 'object' && value.internalname )  { 
       return value.internalname; // assuming it's unique of course
     }
     return `a${counter++}`;
  }
}). 

I took a quick look thru the code to see if I could put it in, but it was starting to get a bit invasive. If you're too busy, I may take another whack at it later in the week.

@eemeli
Copy link
Owner

eemeli commented May 6, 2020

If you were to first create a YAML Document instance with your data as contents (as YAML.stringify does under the hood), you'd have access to the doc.anchors API. That includes at least the setAnchor(node, name) method, which lets you set the anchor even for a node that's already got one. I realise now that its internal map: { [name: string]: Node } isn't a part of the public API, and it's missing a way to iterate through the existing anchors. I should fix that.

Would that work for you? Otherwise, to implement what you suggest, you should look into adding a second arg for doc.anchors.newName() and using that at least from its call in stringify/stringify.js. Its internals would then need to call some user-defined function (coming from a new option) to generate the anchor name.

@eemeli
Copy link
Owner

eemeli commented May 6, 2020

Bother. Mis-thought while writing the commit message. Really the new Anchors method is called getNames().

@eemeli
Copy link
Owner

eemeli commented May 16, 2020

Closing as presumably solved.

@eemeli eemeli closed this as completed May 16, 2020
@fearthecowboy
Copy link
Author

Ah yes, thanks! I think I'm on the right track. (Apologies for not responding, I could have sworn I did, but my brain sometimes composes messages and then forgets to actually tell my fingers.)

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