Skip to content

Conversation

@yucheng11122017
Copy link
Contributor

What is the purpose of this pull request?

  • Documentation update
  • Bug fix
  • Feature addition or enhancement
  • Code maintenance
  • DevOps
  • Improve developer experience
  • Others, please explain:

Part of #1913.
Duplicate of #2226

Overview of changes:
Migrates core/src/lib/markdown-it to TypeScript

Per #1913, the other files in this folder won't be migrated to TS for now because they are mostly from other source codes.

Anything you'd like to highlight/discuss:

Testing instructions:

Proposed commit message: (wrap lines at 72 characters)
Rebase commit


Checklist: ☑️

  • Updated the documentation for feature additions and enhancements
  • Added tests for bug fixes or features
  • Linked all related issues
  • No unrelated changes

Copy link
Contributor

@jovyntls jovyntls left a comment

Choose a reason for hiding this comment

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

Thanks for fixing the weird git issues @yucheng11122017 :)
Left a few comments!

Comment on lines 68 to 69
markdownIt.renderer.rules.fence
= (tokens: Token[], idx: number, options: Options, env: any, slf: Renderer) => {
Copy link
Contributor

@jovyntls jovyntls Mar 23, 2023

Choose a reason for hiding this comment

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

Suggested change
markdownIt.renderer.rules.fence
= (tokens: Token[], idx: number, options: Options, env: any, slf: Renderer) => {
markdownIt.renderer.rules.fence = (tokens: Token[],
idx: number, options: Options, env: any, slf: Renderer) => {

This indentation can help avoid unnecessary whitespace changes throughout the function, which will help git recognise index.ts as a refactor instead of a new file.


return new HighlightRule(components);
const componentsWithoutNull: HighlightRuleComponent[] = [];
components.forEach((c: HighlightRuleComponent | null) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to use filter(component => component !== null) here instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No sadly.. the type is still (HighlightRuleComponent | null) [] after filtering
By right, there shouldn't be any null anyway because of the previous check some(c => !c) but this is not recognised by the linter

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see, thanks for the clarification 😭 In that case could we do a typecast directly in the return (component as HighlightRuleComponent[]) since we're sure that there shouldn't be nulls?

Comment on lines 44 to 48
const lineNumberStr = groups.shift();
if (lineNumberStr === undefined) {
return null;
}
let lineNumber = parseInt(lineNumberStr, 10);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const lineNumberStr = groups.shift();
if (lineNumberStr === undefined) {
return null;
}
let lineNumber = parseInt(lineNumberStr, 10);
let lineNumber = parseInt(groups.shift() ?? '', 10);

Since parseInt('') will be NaN!

Copy link
Contributor

Choose a reason for hiding this comment

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

This file is currently ignored by .eslintignore - can we add it back to ensure that it's linted?

Comment on lines 6 to 10
const key: IconName | undefined = (Object.keys(octicons) as Array<IconName>).find(key => key === iconName);
if (key === undefined) {
return null;
}
return octicons[key];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
const key: IconName | undefined = (Object.keys(octicons) as Array<IconName>).find(key => key === iconName);
if (key === undefined) {
return null;
}
return octicons[key];
return octicons[iconName as IconName];

or if we need to return null instead of undefined:

Suggested change
const key: IconName | undefined = (Object.keys(octicons) as Array<IconName>).find(key => key === iconName);
if (key === undefined) {
return null;
}
return octicons[key];
return octicons[iconName as IconName] ?? null;

Comment on lines 37 to 42
const octiconIconHtml = iconClass
? octiconIcon.toSVG({ class: iconClass })
: octiconIcon.toSVG();
const $ = cheerio.load(octiconIconHtml);
$('svg').attr('style', 'color: #fff');
return $.html();
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there a reason why we are performing this transformation in cheerio instead of passing style as an option in the toSVG method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the interface of SVG options doesn't have the attribute style😭

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah ic, thanks for the workaround!

@yucheng11122017
Copy link
Contributor Author

Thanks for the comments @jovyntls! I updated accordingly

Copy link
Contributor

@jovyntls jovyntls left a comment

Choose a reason for hiding this comment

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

LGTM! lib/markdown-it in TS is nice to have 🎊

@jovyntls jovyntls added this to the v4.1.1 milestone Mar 23, 2023
@jovyntls
Copy link
Contributor

@yucheng11122017 Feel free to try merging this (rebase strategy) once it's ready - after a second approval if possible!

Copy link
Contributor

@raysonkoh raysonkoh left a comment

Choose a reason for hiding this comment

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

LGTM

@yucheng11122017 yucheng11122017 merged commit c876551 into MarkBind:master Mar 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants