Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Show-Markdown cmdlet to handle string array input #20971

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ArmaanMcleod
Copy link
Contributor

@ArmaanMcleod ArmaanMcleod commented Dec 29, 2023

PR Summary

Fixes #20951

Added fix to Show-Markdown to allow pipeline processing of array of strings using -InputObject.

PR Context

Added input object buffer to process strings in EndProcessing() instead of ProcessRecord() so all strings can be collected at once from -InputObject.

This change follows similar approach to buffering in ConvertFrom-Json.

> Get-WhatsNew | Show-Markdown
Release notes for PowerShell 7.4

Breaking changes

- Nano server docker images aren't available for this release
- Added the ProgressAction parameter to the Common Parameters
- Update some PowerShell APIs to throw ArgumentException instead of ArgumentNullException when the argument is an empty string (#19215][19215]) (Thanks @xtqqczze!)
- Remove code related to #requires -pssnapin (#19320][19320])
- Test-Json now uses Json.Schema.Net instead of Newtonsoft.Json.Schema. With this change, Test-Json no longer supports the older Draft 4 schemas. (#18141][18141]) (Thanks @gregsdennis!)
- Output from Test-Connection now includes more detailed information about TCP connection tests

Installer updates

The Windows MSI package now provides an option to disable PowerShell telemetry during installation. For more information, see Install the msi package from the command line][01].

...

PR Checklist

@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 29, 2023
@ArmaanMcleod
Copy link
Contributor Author

@mklement0 @iSazonov

Had some time to look at this again and I see a main challenge.

If we want to combine all input from MarkdownInfo and string into one string, it tricky since MarkdownInfo only exposes Html or VT100EncodedString, and if you call MarkdownRenderer.Convert() on a VT100EncodedString, it will give strange results:

> $mdText = "**Bold**"
> $mdVT100String = $mdText | ConvertFrom-Markdown -AsVT100EncodedString
> $mdVT100String | Format-List

Html               :
VT100EncodedString : Bold # This is bold in terminal, as expected

Tokens             : {Markdig.Syntax.ParagraphBlock}
> [Microsoft.PowerShell.MarkdownRender.MarkdownConverter]::Convert($mdVT100String.VT100EncodedString, [Microsoft.PowerShell.MarkdownRender.MarkdownConversionType]::VT100, [Microsoft.PowerShell.MarkdownRender.PSMarkdownOptionInfo]::new()) | Format-List

Html               :
VT100EncodedString : mBoldm # This has strange characters, almost looking like it was unescaped or something


Tokens             : {Markdig.Syntax.ParagraphBlock}

Which shows some strange VT100EncodedString which seems to be changed. Almost seems that it tries to re-encode the string again, which I guess makes sense.

This above makes it challenging since after combining all strings, you will need to run MarkdownRenderer.Convert() on the single string and not be able to differentiate.

One option I was looking into was seeing if you can get the original markdown from AST with Tokens, but that proved to be quite difficult to parse.

Another option was perhaps running the conversion line by line for string inputs and ignoring MarkdownInfo and adding them as is, but that will get more difficult because Convert seems to add quite of bit of extra new lines so they will need to be normalized. This is probably quite challenging to get right though, could introduce more regression.

With above findings I can see why this was never done in the first place. Perhaps existing approach of just handling strings is not a bad idea, I do consider mixing types in input to be an edge case.

@mklement0
Copy link
Contributor

@ArmaanMcleod, I'm a bit confused: [Microsoft.PowerShell.MarkdownRender.MarkdownConverter]::Convert() expects Markdown as input - i.e. plain text - so it's not surprising that it doesn't handle a string with VT escape sequences meaningfully, and I don't think it should be expected to.

@mklement0
Copy link
Contributor

mklement0 commented Jan 14, 2024

That said, that it effectively breaks such escape sequences instead of passing them through is definitely problematic (seemingly, it removes the [ characters, so that "`e[1mBold`e[0m" turns into "`e1mBold`e0m"), but that strikes me as a separate problem.

@iSazonov
Copy link
Collaborator

I agree with @mklement0 I can't think of a realistic scenario for such a transformation.

@ArmaanMcleod ArmaanMcleod changed the title Fix Show-Markdown cmdlet to handle string array input WIP: Fix Show-Markdown cmdlet to handle string array input Jan 14, 2024
@ArmaanMcleod ArmaanMcleod marked this pull request as draft January 14, 2024 19:49
@ArmaanMcleod
Copy link
Contributor Author

Thanks @mklement0 & @iSazonov

In this case I don't think it makes sense to try and combine MarkdownInfo strings with string and pass everything to MarkdownRenderer.Convert(), so I've made changes to just collect all input of type string. I don't think you can do both unless there was a way to get original markdown string from MarkdownInfo. Otherwise as shown above we will be passing already VT100 escaped sequences into MarkdownRenderer.Convert() and get strange results.

@ArmaanMcleod ArmaanMcleod changed the title WIP: Fix Show-Markdown cmdlet to handle string array input Show-Markdown cmdlet to handle string array input Jan 14, 2024
@ArmaanMcleod ArmaanMcleod marked this pull request as ready for review January 14, 2024 20:14

This PR has 13 quantified lines of changes. In general, a change size of upto 200 lines is ideal for the best PR experience!


Quantification details

Label      : Extra Small
Size       : +13 -0
Percentile : 5.2%

Total files changed: 2

Change summary by file extension:
.cs : +6 -0
.ps1 : +7 -0

Change counts above are quantified counts, based on the PullRequestQuantifier customizations.

Why proper sizing of changes matters

Optimal pull request sizes drive a better predictable PR flow as they strike a
balance between between PR complexity and PR review overhead. PRs within the
optimal size (typical small, or medium sized PRs) mean:

  • Fast and predictable releases to production:
    • Optimal size changes are more likely to be reviewed faster with fewer
      iterations.
    • Similarity in low PR complexity drives similar review times.
  • Review quality is likely higher as complexity is lower:
    • Bugs are more likely to be detected.
    • Code inconsistencies are more likely to be detected.
  • Knowledge sharing is improved within the participants:
    • Small portions can be assimilated better.
  • Better engineering practices are exercised:
    • Solving big problems by dividing them in well contained, smaller problems.
    • Exercising separation of concerns within the code changes.

What can I do to optimize my changes

  • Use the PullRequestQuantifier to quantify your PR accurately
    • Create a context profile for your repo using the context generator
    • Exclude files that are not necessary to be reviewed or do not increase the review complexity. Example: Autogenerated code, docs, project IDE setting files, binaries, etc. Check out the Excluded section from your prquantifier.yaml context profile.
    • Understand your typical change complexity, drive towards the desired complexity by adjusting the label mapping in your prquantifier.yaml context profile.
    • Only use the labels that matter to you, see context specification to customize your prquantifier.yaml context profile.
  • Change your engineering behaviors
    • For PRs that fall outside of the desired spectrum, review the details and check if:
      • Your PR could be split in smaller, self-contained PRs instead
      • Your PR only solves one particular issue. (For example, don't refactor and code new features in the same PR).

How to interpret the change counts in git diff output

  • One line was added: +1 -0
  • One line was deleted: +0 -1
  • One line was modified: +1 -1 (git diff doesn't know about modified, it will
    interpret that line like one addition plus one deletion)
  • Change percentiles: Change characteristics (addition, deletion, modification)
    of this PR in relation to all other PRs within the repository.


Was this comment helpful? 👍  :ok_hand:  :thumbsdown: (Email)
Customize PullRequestQuantifier for this repository.

@ArmaanMcleod ArmaanMcleod changed the title Show-Markdown cmdlet to handle string array input Fix Show-Markdown cmdlet to handle string array input Jan 14, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Jan 22, 2024
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Extra Small Review - Needed The PR is being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show-Markdown does not handle string array input
4 participants