Supporting Local Images



Our next step is to provide the option to view images stored locally on the user's computer.
Currently, our extension identifies the URL start position by searching for the closest 'HTTP' to the cursor's position.

Local paths don't start with 'HTTP', so we needed to extract the path of the image from the text using other methods.

One of the problems we encountered was that we do not know where the path starts but only where it ends (checking for the file extension closest to the current position).

Another problem was identifying a path of an image when there are 2 paths one after another.
And how we dealt with it:

After searching for the HTTP we also made sure there were no white spaces from the HTTP to the current cursor position.

If there are any white spaces, then there must be 2 (or more) paths in that substring (or the cursor is not standing on a path).

So, to extract the last path (the one we currently stand on), we used the "exec" function with a white space regular expression and kept a variable (last) which holds the current white space position the exec function has found.

By the end of the loop, we have a variable that has the start position of the last path in the substring. The last thing is to sum that position to the original HTTP position.



After we were able to extract the path, we had to figure out how to open a local path of an image.
For paths that start at ‘file:///’, there was no problem- it means that the file is local. But we noticed that if you don’t have this prefix, it doesn’t work.

Therefore, we moved the code of creating the markdown string to a separate function called ‘makeMarkedownString’  that also checks whether the current path starts at ‘file:///’ and if not, it concatenates it to the beginning of the path. 

Also, we realized that the local image path couldn’t be opened on the HTML page because it isn’t an URL address, it's a local path, and HTML is a web page so it doesn’t support that.

But, we saw that it is possible to open it in a new tab by clicking on open in browser, so we added a condition to check if the current path is a local, and if so – we remove the option to open it in a browser and instead we have added the option to open it in a new tab.



And that’s what our new achievement looks like:



Comments

Popular posts from this blog

Turorial- How to add a new feature

Little Bug Fix

Publishing First Beta & Refactoring The Code