What is the vscode command to open a file in preview - visual-studio-code

I wrote an extension for vscode. After installation the extension folder contains documentation in a markdown file. I want to provide a command that loads this file into the preview pane so it displays rendered with images and hyperlinks etc.
You can do this sort of thing interactively:
and I have the full path to the markdown file, so now all I need is details of the command that implements this context menu item.
Web search does not produce complete or usable results.

After cloning the VS Code repo and trawling through the source I discovered the markdown.showPreview and associated commands.
To give credit where due, Lex Li reported the corresponding package.json entry in a comment while I was looking.
Without parameters this previews the content of the active editor, but as I said in a comment, it supports an optional Uri parameter and the code looks like this:
let pathToManual = path.join(context.extensionPath, "manual.md");
let uriManual: vscode.Uri = vscode.Uri.file(pathToManual);
vscode.commands.executeCommand('markdown.showPreview', uriManual);
For information on constructing workspace relative paths see the answer from Mark. The joinPath method he uses requires a base path as a Uri which is conveniently available for the workspace but not for the extension path.
If you need information on things like showing preview to one side then given the dearth of documentation I recommend cloning the repo and searching it for "markdown.showPreview", then exploring nearby code. If you fold the methods it gets easier to survey your options.

Try:
vscode.commands.executeCommand("markdown.showPreview", vscode.Uri.joinPath(vscode.workspace.workspaceFolders[0].uri,'test.md'));
Your fileName there at the end of course. And that assumed you are in the first or only root of a workspace. You might be able to simplify it using:
vscode.Uri.path(<path to file)
instead of the joinPath that I used.

Related

Navigate to area of Markdown file in Azure Devops

I have a markdown file with a number of codes and descriptions within it which will be used like a wiki.
A developer will be given a link, and the link should take them to the specific code within the markdown file.
Example of file:
#IAmAPizza
This declares that you are a pizza
#CordialComrade
You've encountered a cordial comrade who will eat your pizza.
...
#ZestfulZebra
You can't keep up with him, just go with it.
[test code](#test-code)
I am a testing code
I've tried navigating to the file in the repo and then adding #test-code to the address, but it doesn't do anything. I assume that the markdown isn't being rendered on the page.
Is there a way to navigate to a specific # within a file in an Azure Devops Repo?
Is there a way to navigate to a specific # within a file in an Azure Devops Repo?
We could try to open the markdown file in the repo under the Contents view. Then select the specific #, like: #ZestfulZebra:
Just as Hugh pointed, there is an option copy link to this selection. We could get the link after click the hyperlink symbol, it should be:
https://dev.azure.com/<OrganizationName>/_git/<ProjectName>?path=%2FReadme.md&version=GBmaster&line=64&lineEnd=64&lineStartColumn=1&lineEndColumn=14&lineStyle=plain
And the link for the markdown file is:
https://dev.azure.com/<OrganizationName>/_git/<ProjectName>?path=%2FReadme.md&version=GBmaster
So, it use the syntax &line=64&lineEnd=64&lineStartColumn=1&lineEndColumn=14&lineStyle=plain to locate the anchor we selected.
Now, we can simply parse this syntax &line=64&lineEnd=64 is used to locate the line number we select the code. If we change it to &line=64&lineEnd=65, it will select the lines 64 and 65. And the &lineStartColumn=1&lineEndColumn=14 is used to locate the start and end positions of the selected string.
Now, we could use this syntax with the link of markdown file to navigate to a specific # within a file in an Azure Devops Repo.
For example, we could use the web address to navigate the anchors in the MD:
https://dev.azure.com/<OrganizationName>/_git/<ProjectName>?path=%2FReadme.md&version=GBmaster&line=64&lineEnd=64&lineStartColumn=1&lineEndColumn=14&lineStyle=plain
Do you mean adding anchor link in markdown file? If so, I tested this syntax: [test](codeLink) without # and it works well.
I first get the code link in a code file in the repo:
Then add this:[test](https://dev.azure.com/{org}/_git/{repo}?path=%2FStartup.cs&version=GBmaster&line=4&lineEnd=5&lineStartColumn=1&lineEndColumn=1&lineStyle=plain) to markdown file:
Click test in the wiki to successfully navigate to the code:

Is it possible to use nested links in Markdown -> to use in github

I would like to find a way to use nested url links in markdown (or a simple code) to be able to:
have a reference file in GitHub (reference.md) containing all the links that I cite several times in several *.md files in my repository. This to be able to correct broken links in one place without having to crosscheck all my files. Is this possible?
ex:
reference.md file contains: [citat1]:url
files 1.md...2.md..n.md contain code meaning something like: [citat1](lookup and use the url given in reference.md)
so if I click on citat1 on the generated webpage from the different files 1...2 the link would be active and redirect me to the appropriate url.
Anyone can help or point in the right direction? thanks

VS Code Extension for Loading Source Code

I am interested in extending VS Code to load/edit/save project code, files, etc from a place other than the file system. For example, let's say I wanted to store my project in a database. I have looked at the extension API docs but didn't see anything obvious. Is there an API for extending VS Code in this way?
After some looking around it seems like you need to look at the "Workspace" section of the official API docs:
https://code.visualstudio.com/docs/extensionAPI/vscode-api#_workspace
The rootPath variable takes a string argument, but there's nothing saying that it needs to be a file path so perhaps there's some wiggle room there.
That being said the createFileSystemWatcher() method appears to be set up to work with an actual file system. So even if you can get VSC to find files from somewhere like a database - you probably can't use any of the events that update the UI on changing a file.

How do I view the source of Markdown files on Github?

Github helpfully renders Markdown (.md) files to HTML when viewing on github.com (for example, this README.md).
When viewing any other source file, it is shown as unrendered source code (for example, this .gitignore). A handy feature this gives is linking directly to a line in the source by clicking the line number (for example, like this).
How can I view the unrendered source of Markdown files on Github (so I can link to a particular line in the source)?
note: I know of the "Raw" button, however it does not provide any of the nice UI Github has.
If you don't want to use the "raw" button, you can (since June 2021) add ?plain=1 to your GitHub markdown file URL:
Appending ?plain=1 to the url for any Markdown file will now display the file without rendering.
As with other code files, it will also show line numbers, and can be used to link other users to a specific line or lines.
For example, appending ?plain=1#L52 will highlight line 52 of a plain text Markdown file.
Example:
https://github.com/git/git/blob/master/README.md?plain=1#L49-L51
Since Sept. 2021, there is a button which adds the ?plain=1 for you:
Use the "Raw" button. For instance, if you were looking at README.md there is a Raw button in the Navigation. This can be simple URL change (from blob to raw), but in this case becomes a separate link.
Edit: There is no "pretty" view for the Markdown source. There's the converted HTML view, the Raw view without the UI, and the Blame view. Only the Blame view provides you with the ability to link to a specific line within the source.
Edit 2: The other option is to use Anchors within the README.md. If you take the Installation title for example, you can directly link within the content of the Markdown file. Assuming you don't have anything hidden within the Markdown itself, this is closer still to solving your original question.
Using the "Blame" button, you are able to see the source of the Markdown (for example, like this).
Unfortunately, "Blame" comes with extra UI elements which make the source more cluttered.
Refined Github provides an additional "Raw" button to view the raw source and link to a specific line:
I ran into this issue, as a complete surprise to me, on a file with a .wiki extension. It appears that files with .wiki, .mediawiki, and .wikitext extensions are recognized and rendered as HTML, with a table of contents even. I guess it makes sense, what with GitHub projects having wikis and all.
That's nifty, but this one particular case, I really wanted the raw wikitext to be visible and copy-pasteable, not some rendered representation of it.
The only half-way solution I found was to use a bogus file extension that isn't one of the ones recognized by GitHub, and then a .gitattributes to tell GitHub how to syntax highlight it, e.g.:
*.wiki_ linguist-language=mediawiki
I'm not thrilled about this, but all the good extensions were already "taken" according to the languages.yml linked above.

GitHub relative link in Markdown file

Is there a way to create a URL anchor, <a>, link from within a Markdown file, to another file within the same repository and branch (aka a link relative to the current branch)?
For example, in the master branch I have a README.md file, which I would like do something like:
# My Project
is really really cool. My Project has a subdir named myLib, see below.
## myLib documentation
see documentation [here](myLib/README.md)
This would allow me to link from one .md to another within the same branch and not have to worry about which branch I'm in (avoid having to do an absolute URL that includes the github.com branch name).
Here is a working example of what I mean:
GOTO http://github.com/rynop/testRel, link does not work.
GOTO http://github.com/rynop/testRel/blob/master/README.md, link works.
This is expected because at this point the starting URL is in the branch. Now how do I get it to pick up the current branch in the README.md at the root of the repository?
Update: I opened an issue against GitHub for this feature request.
Update 30th, January 2013, 16 months later:
GitHub Blog Post Relative links in markup files:
Starting today, GitHub supports relative links in markup files.
Now you can link directly between different documentation files, whether you view the documentation on GitHub itself, or locally, using a different markup renderer.
You want examples of link definitions and how they work? Here's some Markdown for you.
Instead of an absolute link:
[a link](https://github.com/user/repo/blob/branch/other_file.md)
…you can use a relative link:
[a relative link](other_file.md)
[a relative link](path%20with%20spaces/other_file.md)
and we'll make sure it gets linked to user/repo/blob/branch/other_file.md.
If you were using a workaround like [a workaround link](repo/blob/master/other_file.md), you'll have to update your documentation to use the new syntax.
This also means your documentation can now easily stand on its own, without always pointing to GitHub.
Marcono1234 adds in the comments
Also useful: When the link starts with a /, it is relative to the root of the repository (regardless of whether the markdown file is nested in subdirectories)
Update December 20th, 2011:
The GitHub markup issue 84 is currently closed by technoweenie, with the comment:
We tried adding a <base> tag for this, but it causes problems with other relative links on the site.
October 12th, 2011:
If you look at the raw source of the README.md of Markdown itself(!), relative paths don't seem to be supported.
You will find references like:
[r2h]: http://github.com/github/markup/tree/master/lib/github/commands/rest2html
[r2hc]: http://github.com/github/markup/tree/master/lib/github/markups.rb#L13
As noted in InvisibleWolf's answer, if the target link is a directory and it has space, then you need to use %20 for each space.
For example, you have a repo like the following:
project/
text.md
subpro/
subtext.md
subsubpro/
subsubtext.md
subsubpro2/
subsubtext2.md
The relative link to subtext.md in text.md might look like this:
[this subtext](subpro/subtext.md)
The relative link to subsubtext.md in text.md might look like this:
[this subsubtext](subpro/subsubpro/subsubtext.md)
The relative link to subtext.md in subsubtext.md might look like this:
[this subtext](../subtext.md)
The relative link to subsubtext2.md in subsubtext.md might look like this:
[this subsubtext2](../subsubpro2/subsubtext2.md)
The relative link to text.md in subsubtext.md might look like this:
[this text](../../text.md)
GitHub could make this a lot better with minimal work. Here is a work-around.
I think you want something more like
[Your Title](your-project-name/tree/master/your-subfolder)
or to point to the README itself
[README](your-project-name/blob/master/your-subfolder/README.md)
As of January 31, 2013 Github markdown supports relative links to files.
[a relative link](markdown_file.md)
However, there are a few deficiencies that have been discussed in this comment thread.
As an alternative, you can use Gitdown to construct full URLs to the repository and even make them branch aware, e.g.
{"gitdown": "gitinfo", "name": "url"} // https://github.com/gajus/gitdown
{"gitdown": "gitinfo", "name": "branch"} // master
Gitdown is a GitHub markdown preprocessor. It streamlines common tasks associated with maintaining a documentation page for a GitHub repository, e.g. generating table of contents, including variables, generating URLs and getting information about the repository itself at the time of processing the input. Gitdown seamlessly integrates with your building scripts.
I am the author of the Gitdown library.
Just wanted to add this because none of the above solutions worked if target link is directory with spaces in it's name. If target link is a directory and it has space then even escaping space with \ doesn't render the link on Github. Only solution worked for me is using %20 for each space.
e.g.: if directory structure is this
Top_dir
|-----README.md
|-----Cur_dir1
|----Dir A
|----README.md
|----Dir B
|----README.md
To make link to Dir A in README.md present in Top_dir you can do this:
[Dir 1](Cur_dir1/Dir%20A)
You can link to file, but not to folders, and keep in mind that, Github will add /blob/master/ before your relative link(and folders lacks that part so they cannot be linked, neither with HTML <a> tags or Markdown link).
So, if we have a file in myrepo/src/Test.java, it will have a url like:
https://github.com/WesternGun/myrepo/blob/master/src/Test.java
And to link it in the readme file, we can use:
[This is a link](src/Test.java)
or: This is a link.
(I guess, master represents the master branch and it differs when the file is in another branch.)
You can use relative URLs from the root of your repo with <a href="">. Assuming your repo is named testRel, put the following in testRel/README.md:
# My Project
is really really cool. My Project has a subdir named myLib, see below.
## myLib docs
see documentation:
* myLib/
* myLib/README.md
This question is pretty old, but it still seems important, as it isn't easy to put relative references from readme.md to wiki pages on Github.
I played around a little bit and this relative link seems to work pretty well:
[Your wiki page](../../wiki/your-wiki-page)
The two ../ will remove /blob/master/ and use your base as a starting point. I haven't tried this on other repositories than Github, though (there may be compatibility issues).
In .md file when you want to create a link to any related files which is related to same project folder or URL based links:
Existing file link:
In .md file add showing name in [] box then open () in this box add / after that it will auto fetch all existing folders name form which you choose your folder then add '/' it will show inner file or 'folders`. This means it will auto fetch the paths and at the end, your URL is created.
Example:
[Enter Showing Name](/users/4000266/heysushil)
This line show like this:
Enter Showing Name
Add URLs on .md file
Follow the same process only change the () box path with URL like this:
[HeySushil - YouTube](https://youtube.com/heysushil)
This looks like HeySushil - YouTube
Note: Remember no need to add force manually after adding / on () box it will auto fetch all existing folders and files.
I am not sure if I see this option here. You can just create a /folder in your repository and use it directly:
[a relative link](/folder/myrelativefile.md)
No blob or tree or repository name is needed, and it works like a charm.
If you want a relative link to your wiki page on GitHub, use this:
Read here: [Some other wiki page](path/to/some-other-wiki-page)
If you want a link to a file in the repository, let us say, to reference some header file, and the wiki page is at the root of the wiki, use this:
Read here: [myheader.h](../tree/master/path/to/myheader.h)
The rationale for the last is to skip the "/wiki" path with "../", and go to the master branch in the repository tree without specifying the repository name, that may change in the future.
The behaviour in Github directory links in markdown files could create some headaches.
All files have an url are starting with blob,
blob/<branch>/<path>/<fileName>
whereas directories have an url starting with tree:
tree/<branch>/<path>
So no relative link to a directory should work. It points to an url that starts with a blob. Magically Github changes some relative path automatically to the tree path. Unfortunately not urls containing ".."
I investigate some time to examine the strange behaviour with Github directory links on https://github.com/fluentcodes/GitSamples-GIT/tree/github-readme-links.
Just follow the format below.
[TEXT TO SHOW](actual URL to navigate)
Here is what I found for relative links in github's .md files:
Relative links without ../ (aka. link to file in the same level, or in sub dir), seems always work.
Relative links that contains ../ works only when you are already in a page whose url contains substring /blob/.
Otherwise need to use full url that contains /blob/, to link.
Actually except top level /README.md, when view any other files, their url already contains /blob/.
Thus probably you can always use relative links that contains ../ in files, except /README.md.
But /README.md is in top level, thus it shouldn't need a relative link that contains ../.
So, you should never need a full path link (that contains github domain) to link to files in the same github repo.
Example (links among multiple language doc)
In /README.md, can use relative links without ../.
[中文](doc/cn/README.md) |
[日本語](doc/jp/README.md)
In /doc/cn/README.md, since when we view it, the url already contains /blob/, thus could use relative links with ../.
[English](../../README.md) |
[日本語](../jp/README.md)
(BTW, the example come from this git-merge-flow repo.)
What I found to work for relative folders:
[link text](subfolder/subsubfolder/relevantfolder/)
where relevant folder is the folder you are trying to link to. The backslash is needed after the folder name. I tried including the project name and tree/main in the path and the other things suggested in previous answers, that lead to a nonexistent page error. The subfolder is a folder within the overall repo project folder.
Thus, file links would be in the form of:
[link text](subfolder/subsubfolder/relevantfolder/index.html)
If the desired file is not in any folder or subfolder the link is simply:
[link text](README.md)
for example the README file is not in any subfolders, so that could be the exact relative link you use for the README file.
You can also insert your links (relative or absolute) at the bottom to reduce clutter in text.
It can also be a good option for repetitive links.
This is [my linked text].
[my linked text]: relative/path/in/repo/to/file.txt "Optional title for mouse hover"
This is my linked text.