Skip to content

Conversation

@chrisjsewell
Copy link
Member

Add additional configuration presets,
allow for options tp be overriden in the MarkdownIt initialisation,
Add convenience methods to SyntaxTreeNode

Add additional configuration presets, allow for options tp be overriden in the `MarkdownIt` initialisation, Add convenience methods to `SyntaxTreeNode`
@chrisjsewell chrisjsewell requested a review from hukkin March 3, 2021 17:21
@chrisjsewell chrisjsewell linked an issue Mar 3, 2021 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Mar 3, 2021

Codecov Report

Merging #136 (77a9833) into master (a70db2a) will increase coverage by 0.10%.
The diff coverage is 90.19%.

Impacted file tree graph

@@ Coverage Diff @@ ## master #136 +/- ## ========================================== + Coverage 96.16% 96.27% +0.10%  ========================================== Files 72 72 Lines 3107 3141 +34 ========================================== + Hits 2988 3024 +36  + Misses 119 117 -2 
Flag Coverage Δ
pytests 96.27% <90.19%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
markdown_it/presets/commonmark.py 100.00% <ø> (ø)
markdown_it/renderer.py 97.16% <ø> (ø)
markdown_it/main.py 90.22% <80.00%> (-1.25%) ⬇️
markdown_it/tree.py 95.36% <95.00%> (+3.69%) ⬆️
markdown_it/presets/__init__.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a70db2a...3b57e5b. Read the comment docs.

@chrisjsewell
Copy link
Member Author

chrisjsewell commented Mar 3, 2021

@hukkinj1 I'm keeping commonmark as the default, but have added the option override and some other presets

Copy link
Contributor

@hukkin hukkin left a comment

Choose a reason for hiding this comment

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

Nice 👍 , here's my notes!

def __repr__(self) -> str:
return f"{self.__class__.__name__}({self.type})"

def __getitem__(self, item: int) -> "SyntaxTreeNode":
Copy link
Contributor

Choose a reason for hiding this comment

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

I personally favor having just one way to do things, and would prefer the more explicit .children[i] syntax. Don't feel too strongly about that though.

If we keep this though, we might want to improve type annotations. item can be a slice in addition to int and return type will change accordingly too. An @overload is probably needed.

The return type will also break if subclassed. Merging #131 and changing the type to _T (and annotating self as _T) should fix this.

Copy link
Member Author

Choose a reason for hiding this comment

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

yeh exactly, I was going to leave this type annotation stuff to the other PR

return tokens

@property
def is_root(self) -> bool:
Copy link
Contributor

Choose a reason for hiding this comment

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

We could use self.is_root inside def type() definition to remove duplication.

)
child._set_children_from_tokens(nested_tokens[1:-1])

def pretty(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we print closing tags or add the slash character to make the tags self-closing if there's no content? That would make the output more or less valid XML and wouldn't confuse highlighters, parsers etc.

Copy link
Member Author

@chrisjsewell chrisjsewell Mar 3, 2021

Choose a reason for hiding this comment

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

I'm not adding closing tags as the indentation denotes the nesting and this would make it overly verbose and less readable (which is the main point of this function). I could add self-closing, but TBH the highlighting below in tests/test_tree/test_pretty.xml seems to be fine

> a *quote*
""")
node = SyntaxTreeNode.from_tokens(tokens)
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a heads-up, I made a PR (#132) which would change this syntax to node = SyntaxTreeNode(tokens) so if we merge that then one of the PRs needs to adapt depending on merge order.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'll have a look

Comment on lines 35 to 37
_PRESETS["js-default"] = _PRESETS["default"]
_PRESETS["gfm-like"] = presets.default.make()
_PRESETS["gfm-like"]["options"]["linkify"] = True
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make these new presets public in markdown_it.presets package like the original ones are? That enables a workflow where you fetch a preset from there, mutate it, and pass it on to .configure()

Copy link
Member Author

Choose a reason for hiding this comment

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

done in bc3a2ef

self, config: Union[str, Mapping] = "commonmark", renderer_cls=RendererHTML
self,
config: Union[str, Mapping] = "commonmark",
options_update: Optional[Mapping] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

One neat trick I've recently started to use in my APIs is

from types import MappingProxyType # This is basically just an immutable empty mapping EMPTY_MAP = MappingProxyType({}) def func(options: Mapping = EMPTY_MAP): ...

This convention saves me a bunch of Optional annotations and None handling, and avoids the dangerous default problem.

Could be applied here if you like it 😄 You basically just need to create one global EMPTY_MAP instance per project and can reuse it.

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for the tip, I will have a look but probably not add it in this PR

chrisjsewell and others added 3 commits March 4, 2021 00:12
Co-authored-by: Taneli Hukkinen <hukkinj1@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants