Skip to content

Fix formatting, vt100 support, help uri and positional parameter issues for Markdown cmdlets - #7329

Merged
Dongbo Wang (daxian-dbw) merged 10 commits into
PowerShell:masterfrom
adityapatwardhan:FixMarkdownCmdletIssues
Jul 28, 2018
Merged

Dongbo Wang (daxian-dbw) merged 10 commits into
PowerShell:masterfrom
adityapatwardhan:FixMarkdownCmdletIssues

Conversation

@adityapatwardhan

Copy link
Copy Markdown
Member

Fix #7283

PR Summary

  • Formatting changes moved from types file to formatting file.
  • Add logic to check VT100 support before adding VT100 escape sequences.
  • Add online help uris.
  • Add positional parameters for Convert-FromMarkdown and Set-MarkdownOption

PR Checklist

…meter issues for Markdown cmdlets

* Formatting changes moved from types file to formatting file.
* Add logic to check VT100 support before adding VT100 escape sequences.
* Add online help uris.
* Add positional parameters for Convert-FromMarkdown and Set-MarkdownOption
public string FormatHeader2(string headerText)
{
return string.Concat(Esc, options.Header2, headerText, endSequence);
if (options.EnableVT100Encoding)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a lot of similar code, perhaps have a helper that does the EnableVT100Encoding check and concats or just returns the string?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed for header types. For others the code is slightly different for each so kept it as is.

@iSazonov

Copy link
Copy Markdown
Collaborator

Usually we use

[Alias("PSPath", "LP")]

for LiteralPath parameters.

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment

@@ -65,6 +65,11 @@ protected override void BeginProcessing()
{
mdOption = SessionState.PSVariable.GetValue("MarkdownOptionInfo", new MarkdownOptionInfo()) as MarkdownOptionInfo;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about "MarkdownOptionInfo" -> "PSMarkdownOptionInfo"?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer MarkdownOptionInfo since the cmdlet is Get-MarkdownOption. Dongbo Wang (@daxian-dbw) your thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for any variables that are part of PowerShell itself, it should have the PS prefix to avoid name collision. So this should be PSMarkdownOptionInfo. cc Joey Aiello (@joeyaiello)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable MarkdownOptionInfo should be saved in the module scope of the Utility module, instead of the current scope of the current session state. Doing the latter will make the persistence of markdown option unreliable/inconsistent.

And, did you consider other options to persist the markdown option? I don't have an alternative off the top of my mind, but would like to check if you considered anything else.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Steve Lee (@SteveL-MSFT) if we agree it should be moved to the module state, does it matter?

As for persistence, I think it's okay to tell people to put this stuff in their profile until we hear why that's not good enough (seems to work just fine for PSReadline).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dongbo Wang (@daxian-dbw) The committee feedback was to have it in session state so it is valid throughout the session. The preferences can be set in profile for persistence.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm OK to have it in session state, but it should be the module session state of Microsoft.PowerShell.Utility instead of the currently active session state, which could be the global session state or another module's session state. And the scope to save the variable should be the module scope, not the current scope.

For example, if I'm running Set-MarkdownOptions in a function from another module, then the variable will be saved in the function scope under the session state of that module, which will be lost once the function finishes execution.

@rkeithhill

Copy link
Copy Markdown
Collaborator

👍 for positional parameter support. When ConvertFrom-Markdown .\README.md generated an error, I was bummed.

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Steve Lee (@SteveL-MSFT) please have another look.

{
mdOption = SessionState.PSVariable.GetValue("MarkdownOptionInfo", new MarkdownOptionInfo()) as MarkdownOptionInfo;

if (!this.Host.UI.SupportsVirtualTerminal)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.Host can be null if the cmdlet is invoked in a runspace without a PSHost.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@@ -65,6 +65,11 @@ protected override void BeginProcessing()
{
mdOption = SessionState.PSVariable.GetValue("MarkdownOptionInfo", new MarkdownOptionInfo()) as MarkdownOptionInfo;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think for any variables that are part of PowerShell itself, it should have the PS prefix to avoid name collision. So this should be PSMarkdownOptionInfo. cc Joey Aiello (@joeyaiello)

@SteveL-MSFT

Copy link
Copy Markdown
Member

On macOS using the Terminal app, it doesn't seem to display the VT100 inverted sequence so the text becomes unreadable since the color is supposed to be black on inverted background but just appears as black. A basic web search doesn't find anything on how to support this so we may need to special case macOS to use some other colors.

@SteveL-MSFT

Copy link
Copy Markdown
Member

I take back part of what I said. Inverse works for Headers like

# header

But this only renders as black text on black background

`text`

@adityapatwardhan

Copy link
Copy Markdown
Member Author

protected override void BeginProcessing()
{
mdOption = SessionState.PSVariable.GetValue("MarkdownOptionInfo", new MarkdownOptionInfo()) as MarkdownOptionInfo;
mdOption = SessionState.PSVariable.GetValue("PSMarkdownOptionInfo", new PSMarkdownOptionInfo()) as PSMarkdownOptionInfo;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as in #7329 (comment)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

private const string Header6Dark = "[4;97m";
private const string CodeDark = "[48;2;155;155;155;38;2;30;30;30m";

private const string CodeMacOS = "[107;95m";

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should add a comment why we are special casing macOS

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comment.

throw new InvalidOperationException();
}

if (this.Host != null && this.Host.UI.SupportsVirtualTerminal)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.Host.UI.SupportsVirtualTerminal is not needed twice here. But it seems like the logic should be:

mdOption.EnableVT100Encoding = mdOption.EnableVT100Encoding && (this.Host != null) && (this.Host.UI.SupportsVirtualTerminal);

If there is no host I assume EnableVT100Encoding option should be disabled?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Paul Higinbotham (@PaulHigin) improved it a bit by using nullable bool.

@adityapatwardhan

Copy link
Copy Markdown
Member Author


// supportsVT100 == null if the host is null.
// supportsVT100 == false if host does not support VT100.
if (supportsVT100 == null || supportsVT100 == false)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit. I think you can just do if (supportsVT100 != true) here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@SteveL-MSFT

Copy link
Copy Markdown
Member

Aditya Patwardhan (@adityapatwardhan) can you review the CodeFactor issues?

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Steve Lee (@SteveL-MSFT) I have fixed a few CodeFactor issues. Others are either following a pattern which already exists or a rule we want to disable.

Dongbo Wang (@daxian-dbw) Can you have another look?

@daxian-dbw Dongbo Wang (daxian-dbw) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more comment. Otherwise look good to me.

It "Verify PSMarkdownOptionInfo is defined in module scope" {

$mod = Get-Module Microsoft.PowerShell.Utility
$options = & $mod { $PSMarkdownOptionInfo }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better add one check for $PSMarkdownOptionInfo -eq $null before this line, to make sure that the variable is not leaked into the global scope or current scope.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@iSazonov

Copy link
Copy Markdown
Collaborator

Dongbo Wang (@daxian-dbw) Is your #7329 (comment) closed?

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Dongbo Wang (@daxian-dbw) Ready to merge?

@daxian-dbw

Copy link
Copy Markdown
Member

Ilya (@iSazonov) Yes, it has been changed to use the module scope to save the variable.

@daxian-dbw
Dongbo Wang (daxian-dbw) merged commit 4e7c2ed into PowerShell:master Jul 28, 2018
@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) deleted the FixMarkdownCmdletIssues branch July 28, 2018 15:45
@kilasuit Ryan Yates (kilasuit) added WG-Cmdlets general cmdlet issues and removed WG-Cmdlets-Utility labels Jul 17, 2026
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
- Formatting file should be used for `MarkdownOptionInfo` object instead of the types file for adding script property.
- Downlevel windows consoles which do not support VT100 escape sequences should be handled gracefully.
- Add helpuri fwlink for cmdlets.
- Make `-Path` a positional parameter for `ConvertFrom-Markdown`
- make `-InputObject` a positional parameter for `Set-MarkdownOption`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

WG-Cmdlets general cmdlet issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Markdown rendering issues with formatting and down level support

9 participants