• 0 Posts
  • 5 Comments
Joined 9 months ago
cake
Cake day: May 9th, 2024

help-circle
  • I did it!! It also handles the case where an external link and internal link are on the same line :D

    sed -E ':l;s/(\[[^]]*\]\()([^)#]*#[^)]*\))/\1\n\2/;Te;H;g;s/\n//;s/\n.*//;x;s/.*\n//;/^https?:/!{:h;s/^([^#]*#[^)]*)(%20|\.)([^)]*\))/\1-\3/;th;s/(#[^)]*\))/\L\1/;};tl;:e;H;z;x;s/\n//;'
    

    Here is my annotated file

    # Begin loop
    :l;
    
    # Bisect first link in pattern space into pattern space and append to hold space
    # Example: `text [label](file#fragment)'
    #   Pattern space: `file#fragment)'
    #   Hold space: `text [label]('
    # Steps:
    #   1. Strategically insert \n
    #       1a. If this fails, branch out
    #   2. Append to hold space (this creates two \n's. It feels weird for the
    #      first iteration, but that's ok)
    #   3. Copy hold space to pattern space, remove first \n, then trim off
    #      everything past the second \n
    #   4. Swap pattern/hold, and trim off everything up to and incl the last \n
    s/(\[[^]]*\]\()([^)#]*#[^)]*\))/\1\n\2/;
    Te;
    H;
    g; s/\n//; s/\n.*//;
    x; s/.*\n//;
    
    # Modify only if it is an internal link
    /^https?:/! {
        # Add hyphens
        :h;
        s/^([^#]*#[^)]*)(%20|\.)([^)]*\))/\1-\3/;
        th;
        # Make lowercase
        s/(#[^)]*\))/\L\1/;
    };
    
    # "conditional" branch so it checks the next conditional again
    tl;
    
    # Exit: join pattern space to hold space, then move to pattern space.
    # Since the loop uses H instead of h, have to make sure hold space is empty
    :e;
    H;
    z;
    x; s/\n//;
    

  • Why you assume there’s only one link in the line?

    They did not want external (http) links to be modified as that would break it:

    • [Example](https://example.com/#Some%20Link)
    • [Example](https://example.com/#some-link)

    I compromised by thinking that it might be unlikely enough to have an external http link AND internal link within the same line. You could probably still do it, my first thought was [^h][^t][^t][^p] but that would cause issues for #ttp and #A so i just gave up. Instead I think you’d want a different approach, like breaking each link onto their own line, do the same external/internal check before the substitution, and join the lines afterward.

    Also, you perform substitutions in the whole URL instead of the fragment component

    That requirement i missed. I just assumed the filename would be replaced the same way too Lol. Not too hard to fix tho :)


  • annotated it is working like this:

    # use a loop to iteratively replace the %20 with -, since doing s/%20/-/g would replace too much. we loop until it cant substitute any more
    
    # label for looping
    :loop;
    # skip the following substitute command if the line contains an http link in markdown format
    /\[[^]]*\](http/!
    # capture each part of the link, and join it together with -
    s/\(\[[^]]*\]\)\(([^)]*\)%20\([^)]*)\)/\1\2-\3/g;
    # if the substitution made a change, loop again, otherwise break
    t loop;
    
    # convert all insides to the link lowercase if the line doesnt contain an http link
    /\[[^]]*\](http/!
    # this is outside the loop rather than in the s command above because if the link doesnt contain %20 at all then it won't convert to lowercase
    s/\(\[[^]]*\]\)\(([^)]*)\)/\1\L\2/g
    

  • This is very close

    sed ':loop;/\[[^]]*\](http/! s/\(\[[^]]*\]\)\(([^)]*\)%20\([^)]*)\)/\1\2-\3/g;t loop;/\[[^]]*\](http/! s/\(\[[^]]*\]\)\(([^)]*)\)/\1\L\2/g'
    

    example file

    [Some text](#Header%20Linking%20MARKDOWN.md)
    (#Should%20stay%20as%20is.md)
    Text surrounding [a link](readme.md#Other%20Page). Cool
    Multiple [links](#Links.md) in (%20) [a](#An%20A.md) SINGLE [line](#Lines.md)
    Do [NOT](https://example.com/URL%20Should%20Be%20Untouched.html) CHANGE%20 [hyperlinks](http://example.com/No%20Touchy.html)
    

    but it doesn’t work if you have a http link and markdown link in the same line, and doesn’t work with [escaped \] square brackets](#and-escaped-\)-parenthesis) in the link

    but!! it was fun!