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

Replacing LinkInline with reference form - how? #752

Open
NetTecture opened this issue Nov 25, 2023 · 6 comments
Open

Replacing LinkInline with reference form - how? #752

NetTecture opened this issue Nov 25, 2023 · 6 comments
Labels

Comments

@NetTecture
Copy link

Trying to find documentation and I get the idea I am either not finding it - but neither is my AI.

I need to import a markdown document (thousands actually) and extract all the links so they can be maintained separately.

I found them using

    foreach (var node in document.Descendants())
    {
        if (node is LinkInline link)
        {
            var url = link.Url;
            //var linkText = link.FirstChild.ToString();

probem is- what do I replace them with if I then add the references? I have the key - i Just have no idea what to put there.

It seems nto toto be a LinkReferenceDefinition - that is for the references. SO what do I replace it with? The LinkReferenceDefinition can not be it, or?

@NetTecture
Copy link
Author

ok, this seems not to work at all - and the little documentation that there is is never high level.

I try to isolate links and put them back using references..

My current code:

    foreach (var node in document.Descendants())
    {
        if (node is LinkInline link)
        {
            //var linkText = link.FirstChild.ToString();
            var referenceKey = Guid.NewGuid(); // Generate a unique reference key



            LinkReferenceDefinition ldr = new LinkReferenceDefinition(referenceKey.ToString(), link.Url, link.Title);
            document.SetLinkReferenceDefinition(referenceKey.ToString(), ldr, true);

            var newlink = new LinkInline()
            {
                Title = link.Title,
                Reference = ldr,
                LinkRefDefLabel = link.Label,
            };
            // Copy the text (children) from the original link to the new link
            var children = link.ToArray();
            foreach (var child in children)
            {
                child.Remove();
                newlink.AppendChild(child);
            }
            link.ReplaceBy(newlink);
        }
    }

results in links being replaced with [] - no url inside, no reference group being written to the end. What am I doing wrong? To my understanding creating a new link and putting it in place of the old one is the way to go, and as I re-hook the content (child) - that should also work?

I tried the documentation, but there is only it seems a property by property without any higher-level explanation or examples, so one is left to - not even sure what. I do not even understand why the LinkRefrerenceDefinition is not rendered at the end - it IS in the document.

@xoofx xoofx added the question label Nov 28, 2023
@xoofx
Copy link
Owner

xoofx commented Nov 28, 2023

I need to import a markdown document (thousands actually) and extract all the links so they can be maintained separately.
probem is- what do I replace them with if I then add the references? I have the key - i Just have no idea what to put there.

Both statements are confusing. What is the link between the two? Extracting links in the document is one problem. But you are also saying that you are rewriting the document. Is it for generating HTML or to generate back Markdown? This is not clear.

@NetTecture
Copy link
Author

NetTecture commented Nov 28, 2023

I am generating markdown back - the issue is that I am extracing the links because it is easier to then replace them.

This is for moving documents into a complex autonomous storage system. THe document (in markdown) comes in with a link. By taking the link OUT and putting them into a reference, WHILE doing that I can as well write it into a database , then, when actually writing the document into the database, write it without the link references. Upon generating for output, the then CURRENT links (the URL's they point to may change) are easily reconsstructed. The business problem here is that the input markdown is in one language, but the otuput system must handle references to translated langauges - which all have their own URL, hence the target URL is put in when presenting.
All that, though, is imho quite irrelevant to the problem. The problem is taking inline links and moving them so that they are link references in the output which are then - when the final HTML is generated - has the reference part reconstructed by the database.

@xoofx
Copy link
Owner

xoofx commented Nov 29, 2023

Looking at the code, LinkReferenceDefinition (and its group) are not well supported for roundtrip. It would require quite some work to support them. Workaround is to generate the reference definition by yourself at the end, as you collect the links, you can generate this to Markdown. It should not be much work.

@NetTecture
Copy link
Author

Well, I have no problem dumping the reference definitions myself - but my problem is that I have no idea how to get the LINKS to show them. Remember, Links that refer to references are in [], not in () - and I totally fail to genrate a link that does emit the proper reference code.

@xoofx
Copy link
Owner

xoofx commented Nov 29, 2023

This is the renderer used for generating back top Markdown.

You have to look at this file to see what you should modify on a LinkInline. For example, the Title or the url and most of other properties that you are using were only meant for HTML processing. Later a bunch of other properties were added to cover the case of exact roundtrip.

So, yeah, it's messy but there is no other way than to sneak around in the codebase to see how to use this. It was not designed in the first place to support this scenario. I never had a personal incentive to do it, so the person that brought it did his best for his use case, but it's far from perfect. Doing it properly would require a major refactoring/redesign of Markdig, and who wants to do that? It is multiple weeks of fulltime work, and as this project is OSS, you know, that's what you get for free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants