Repository navigation
refactor(markdown-toolkit): read internals only from a known element - #197
Merged
Merged
Conversation
The type holds the non-public side of an element: its syntax tree and the style to write it out with. Nothing in it has been rendered, so "Rendering" named the wrong thing, and the key, the function and the local variables all took that name from it. The type is now `MarkdownInternals`, and the rest follow: `markdownInternalsKey`, `markdownInternalsOf` and `markdownInternals`. No behavior changes.
Called with an element, the function now returns its internals without `undefined` in the type. Called with anything else, it still returns `undefined` for a non-element. Seven callers already hold an element, so their `?.` and `?? []` fallbacks could never run. They are gone.
`markdownInternalsOf` now has one signature: it takes an element and returns its internals. It no longer doubles as the test for "is this an element", so the overload that returned `undefined` is gone. The three places that asserted through it test first: - `substitutePlaceholders` calls the new `assertIsMDElement`, which throws the same TypeError and message as before. - `inlineNodes` and `blockNodes` rely on `isKind`, which they already called. The comment guide quoted the old comment as its example of an `undefined` contract, so it now quotes `readFrontmatter`'s.
…verload # Conflicts: # docs/writing-comments.md # packages/markdown-toolkit/src/builder/document.ts # packages/markdown-toolkit/src/builder/list.ts # packages/markdown-toolkit/src/element/content.ts # packages/markdown-toolkit/src/element/element.ts # packages/markdown-toolkit/src/element/index.ts # packages/markdown-toolkit/src/template/substitute.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
markdownInternalsOfnow takes an element and returns its internals, with noundefined. It no longer doubles as the test for "is this an element": callers check first, and a newassertIsMDElementdoes that check where a public function receives a caller's value. No behavior changes.Changes
One signature.
A new assert,
assertIsMDElement(value, expected). It throws aTypeErrorthat names what was expected and what was given, and it narrows the value toMDElement. It has its own tests.Callers that asserted through the read now test first.
substitutePlaceholderscallsassertIsMDElement. The error class and message are the same as before, so its existing test is unchanged.inlineNodesandblockNodesrely onisKind, which they already called.Fallbacks removed. Seven callers read from
thisor from a value just checked withisFrontmatterorisListItem, so their?.and?? []could never run:MarkdownElement.toStringdocumentDocumentElement.frontmatter,withFrontmatter(twice) andwithStylelistGuide example.
docs/writing-comments.mdquoted the old comment as its example of anundefinedcontract. It now quotesreadFrontmatter's comment, which still has one.