I am trying to work through a markdown file, replacing all the image urls. The format of a markdown image url is ![alternative name](imageurl.png)
My regex search finds the first one, returns the location and I replace it, I then cycle through the document until my regex search doesnt find any - i.e its array of match dimensions is empty.
The problem is for some reason it continues to match on "i dont exactly know what". I.e the length of the array returned from the regex search is never 0
location := split[:locationSplit]
bodyRe := regexp.MustCompile(`!\[(.*)\]\((.*)\)`)
indexes := bodyRe.FindStringIndex(body)
fmt.Println("location: ", absoluteFileLocation)
fmt.Println("length: ", indexes)
for len(indexes) != 0 {
fmt.Println("length: ", len(indexes))
imageLocation := body[indexes[0]:indexes[1]]
body = body[:indexes[0]] + imageLocation + body[indexes[1]:]
indexes = indexes[:0]
fmt.Println("length: ", len(indexes))
indexes = bodyRe.FindStringIndex(body)
}
this returns a continual:
length: 2
length: 0
length: 2
length: 0
length: 2
length: 0
length: 2
length: 0
length: 2
the 2's come from the line indexes = bodyRe.FindStringIndex(body)
inside the loop as I set indexes to 0 just before.
Help appreciated
EDIT: Edit due to request for example included. The above method is clearly flawed, this following method works for the first image, but not for the next ones
So I attempted this technique:
(sample markdown file)
some markdown
![image](anImage.png)
more markdown
![image2](anImage2.png)
more markdown & end of document
and the the revised code:
...
...
bodyRe := regexp.MustCompile(`!\[(.*)\]\((.*)\)`)
indexes := bodyRe.FindAllStringSubmatchIndex(body, -1)
for _, j := range(indexes) { //i is the index, j is the element (in this case j = []int )
imageLocation := body[j[4]:j[5]]
body = body[:j[4]] + "/App/Image/?image=" + location + "/" + imageLocation + body[j[5]:]
}
return body
(required output markdown)
some markdown
![image](/App/Image/?image=[location]/anImage.png)
more markdown
![image2](/App/Image/?image=[location]/anImage2.png)
more markdown
end of document
And that works for the first image. But not the second one. The problem is (I think that when that method loops through and replaces the first one, the indexes in body (i.e body[j[4]:j[5]]
) change and so it replaces the second one in the wrong place.
I need to do this so that when the markdown is eventually rendered the image urls point to places where they can be served from.
EDIT: Fixed
Thanks guys. Due to the fact that people struggled to understand what I wanted to do, I suspect that I am going about the problem in a strange way. I have got it working and below is the code snippet that works for any one else looking into this.
Firstly I will explain why I had the problem. I wanted to seperate out the writing of blogs for a site, from the actual maintenance of the site itself. Therefore 'blog writers' were told to write blogs in markdown, with all image tags in the format of ` where all images must be in the same directory as the markdown file itself. Because this directory is not part of the code base of the website itself, the image urls needed replacing with the absolute urls so they could be served. I didnt want this to be something the blog writers needed to worry about.
Everything worked fine for the first image, but because the replacement absolute URL changed the length and therefore the positions of all of the characters in the blog contents, the indexes that the regex found, no longer aligned, so I had to add the new length to the indexes of matches.
adjustment := 0
for _, j := range(indexes) {
imageLocation := body[j[4]+adjustment:j[5]+adjustment]
replacement := "?imageurl=" + url.QueryEscape(location) + "/" + imageLocation
body = body[:j[4] + adjustment] + replacement + body[j[5] + adjustment:]
adjustment += len(replacement) - len(imageLocation)
}