vscode-markdownlint icon indicating copy to clipboard operation
vscode-markdownlint copied to clipboard

Additional language options in config?

Open tarensanders opened this issue 3 years ago • 13 comments

Hi there,

I really like this extension, but I'm running into a problem not dissimilar to #98. In this case my issue is with R Markdown files. I can change the default language for .Rmd files (as described here), but the issue is then I lose any functionality related to R Markdown (for example, custom buttons for sending the .Rmd to Pandoc, automatic render in a side panel on save, etc).

My current workflow is to write with the language set to R Markdown, and then change it at the end to markdown and fix all the linting issues. I'm wondering if, as suggested in this comment if a language tag in the config file is possible?

tarensanders avatar Feb 05 '22 05:02 tarensanders

A bit of context first, apologies if you know all of this already...

Briefly, this extension acts on files that VS Code identifies as using the "markdown" language. It seems the files you are working with have a different language identifier, probably so they can be handled specially, possibly by a custom extension you've installed. It's possible for us to identify and include that language in the list of what this extension supports. HOWEVER, as with the MDX file type in the issue you link, that risks confusing or wrong behavior if the new file type doesn't play by all of the same rules that standard CommonMark Markdown does.

I had a quick look at the cheat sheet for R Markdown here: https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf. It seems to be standard Markdown, but I'd like to be more sure of that before making an impactful change to the extension. Perhaps you are aware of a more formal specification or can find a more definite statement of what RMD files can do?

PS: As I write this, it occurs to me that I could add a setting for users to opt into additional languages. Unfortunately, some aspects of the way Code requires extensions to declare language support make it difficult/awkward to do so. I don't think this is the best path forward.

DavidAnson avatar Feb 05 '22 06:02 DavidAnson

https://github.com/streetsidesoftware/vscode-spell-checker is an example of allowing users to enable the extension on different file types in Code. There is a difference though, since that then writes the config file to opt-in

nschonni avatar Feb 05 '22 06:02 nschonni

My lesser worry is having to update code action provider registration dynamically if language became configurable: https://github.com/DavidAnson/vscode-markdownlint/blob/4d9d81ea6c864c0fd4a7d5479769f41daa837929/extension.js#L960

My bigger worry is that configuring an extension to properly get loaded when its associated language is used as a declarative step, defined in a file that is statically included with the extension: https://github.com/DavidAnson/vscode-markdownlint/blob/4d9d81ea6c864c0fd4a7d5479769f41daa837929/package.json#L68

Maybe activation can be defined on the fly? Perhaps you know? I am not familiar with how that extension works yet.

DavidAnson avatar Feb 05 '22 06:02 DavidAnson

Didn't really go down the rabbit hole, but they have a command pallet option https://github.com/streetsidesoftware/vscode-spell-checker/blob/main/package.json#L205-L208 that then calls a function https://github.com/streetsidesoftware/vscode-spell-checker/blob/ad20c84dc04f28b2d96ff10032fdd302eba33ab6/packages/client/src/commands.ts#L375-L382 but didn't trace further

nschonni avatar Feb 05 '22 06:02 nschonni

HOWEVER, as with the MDX file type in the issue you link, that risks confusing or wrong behavior if the new file type doesn't play by all of the same rules that standard CommonMark Markdown does.

Absolutely - I can see how this would be a problem. To be clear, I'm only suggesting that markdownlint allow users to add additional file extensions or languages to try to lint, but not to do so by default.

I had a quick look at the cheat sheet for R Markdown here: https://www.rstudio.com/wp-content/uploads/2015/03/rmarkdown-reference.pdf. It seems to be standard Markdown, but I'd like to be more sure of that before making an impactful change to the extension. Perhaps you are aware of a more formal specification or can find a more definite statement of what RMD files can do?

I couldn't find a formal specification, but in the preface to the book about the format, the author of the format gives some details that might help. RMD is really meant to be the same as MD, only that any R code in the file can be converted into raw markdown later. All of this code has to be tagged, and tagged in a way that conforms with markdown.

As a quick sanity check, I ran half a dozen of the most complex Rmd files I have on hand through markdownlint. There were no warnings that contradict Rmd requirements in any of them. MD025 does show up in all of them since typically Rmd gets translated in Pandoc, and the title is different from a first level heading. But, that already has a workaround.

Also, just wanted to note that I'm more than happy to help if I can. I have no real experience with JS though. You might decide that supporting other languages is outside of scope (a totally reasonable decision), but if you do pursue it I'm happy to write docs or provide example Rmd files for tests.

tarensanders avatar Feb 05 '22 09:02 tarensanders

@tarensanders What extension do you use to provide RMD support in VS Code? I have an idea how to do solve this by adding the .RMD extension to the markdown language via the languages contribution point, but I'm not sure that is supported and would need to experiment.

https://code.visualstudio.com/api/references/contribution-points#contributes.languages

DavidAnson avatar Mar 04 '22 07:03 DavidAnson

What extension do you use to provide RMD support in VS Code?

The R language extension also supports RMD. (You can see what's supported here if that's helpful).

tarensanders avatar Mar 06 '22 23:03 tarensanders

My idea above does not work; VS Code seems to require that an extension may only be associated with a single language ID.

diff --git a/package.json b/package.json
index fb4a7e9..057f89f 100644
--- a/package.json
+++ b/package.json
@@ -157,6 +157,14 @@
 				}
 			}
 		],
+		"languages": [
+			{
+				"id": "markdown",
+				"extensions": [
+					".rmd"
+				]
+			}
+		],
 		"snippets": [
 			{
 				"language": "markdown",

DavidAnson avatar Mar 08 '22 04:03 DavidAnson

Adding explicit support for the "rmd" language ID that's defined by the extension you link to does seem to work. I'm not sure how I feel about this, though, as it does not scale well and feels somewhat like a hack. For example, it will only work if that extension is also installed or else the "rmd" language ID won't be defined. And it would need to hardcode whatever language ID a different R extension chooses if there is one. My preference is for VS Code to allow language IDs to be inherited so R Markdown files could be defined as Markdown files with extra stuff and no changes would be needed to this extension.

diff --git a/extension.js b/extension.js
index a2dd969..e6a4726 100644
--- a/extension.js
+++ b/extension.js
@@ -505,7 +505,7 @@ function markdownlintWrapper (document) {
 // Returns if the document is Markdown
 function isMarkdownDocument (document) {
 	return (
-		(document.languageId === markdownLanguageId) &&
+		((document.languageId === markdownLanguageId) || (document.languageId === "rmd")) &&
 		!markdownSchemesToIgnore.has(document.uri.scheme)
 	);
 }
@@ -958,7 +958,10 @@ function activate (context) {
 	);
 
 	// Register CodeActionsProvider
-	const documentSelector = {"language": markdownLanguageId};
+	const documentSelector = [
+		{"language": markdownLanguageId},
+		{"language": "rmd"}
+	];
 	const codeActionProvider = {
 		provideCodeActions
 	};
diff --git a/package.json b/package.json
index fb4a7e9..a217230 100644
--- a/package.json
+++ b/package.json
@@ -66,6 +66,7 @@
 	],
 	"activationEvents": [
 		"onLanguage:markdown",
+		"onLanguage:rmd",
 		"onCommand:markdownlint.fixAll",
 		"onCommand:markdownlint.lintWorkspace",
 		"onCommand:markdownlint.openConfigFile",
@@ -104,7 +105,7 @@
 			"commandPalette": [
 				{
 					"command": "markdownlint.fixAll",
-					"when": "editorLangId == markdown"
+					"when": "editorLangId == markdown || editorLangId == rmd"
 				},
 				{
 					"command": "markdownlint.lintWorkspace",

DavidAnson avatar Mar 08 '22 04:03 DavidAnson