Skip to content

Commit

Permalink
Format the code and improve the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
lamnhan066 committed Sep 21, 2023
1 parent 9d6a974 commit b35e29c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,11 @@ class AttributedTextMarkdownSerializer extends AttributionVisitor {
}
}

/// [DocumentNodeMarkdownSerializer] for serializing [ParagraphNode]s as headers
/// [DocumentNodeMarkdownSerializer] for serializing [ParagraphNode]s as headers.
///
/// We already had a header parser in [ParagraphNodeSerializer] but without the alignment,
/// so we added [HeaderNodeSerializer] to support the alignment for headers. Therefore,
/// the [HeaderNodeSerializer] **MUST** be put before the [ParagraphNodeSerializer].
class HeaderNodeSerializer extends NodeTypedDocumentNodeMarkdownSerializer<ParagraphNode> {
const HeaderNodeSerializer(this.markdownSyntax);

Expand All @@ -376,7 +380,7 @@ class HeaderNodeSerializer extends NodeTypedDocumentNodeMarkdownSerializer<Parag
return null;
}

// Only serialize this node when this is a header node
// Only serialize this node when this is a header node.
final Attribution? blockType = node.getMetadataValue('blockType');
final isHeaderNode = blockType == header1Attribution ||
blockType == header2Attribution ||
Expand Down
18 changes: 11 additions & 7 deletions super_editor_markdown/lib/src/markdown_to_document_parsing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -699,13 +699,17 @@ class _TaskSyntax extends md.BlockSyntax {
}

/// Parses a header preceded by an alignment token.
///
/// We already had a header serializer in [_ParagraphWithAlignmentSyntax] but without the alignment,
/// so we added [_HeaderWithAlignmentSyntax] to support the alignment for headers. Therefore,
/// the [_HeaderWithAlignmentSyntax] **MUST** be put before the [_ParagraphWithAlignmentSyntax].
class _HeaderWithAlignmentSyntax extends md.BlockSyntax {
/// This pattern matches the text alignment notation.
///
/// Possible values are `:---`, `:---:`, `---:` and `-::-`.
static final _alignmentNotationPattern = RegExp(r'^:-{3}|:-{3}:|-{3}:|-::-$');

/// Use internal HeaderSyntax
/// Use internal HeaderSyntax.
final _headerSyntax = const md.HeaderSyntax();

@override
Expand All @@ -716,7 +720,6 @@ class _HeaderWithAlignmentSyntax extends md.BlockSyntax {

@override
bool canParse(md.BlockParser parser) {
//
if (!_alignmentNotationPattern.hasMatch(parser.current)) {
return false;
}
Expand All @@ -726,9 +729,11 @@ class _HeaderWithAlignmentSyntax extends md.BlockSyntax {
// We found a match for a paragraph alignment token. However, the alignment token is the last
// line of content in the document. Therefore, it's not really a paragraph alignment token, and we
// should treat it as regular content.
if (nextLine == null) return false;
if (nextLine == null) {
return false;
}

// Only parse if the next line is header
// Only parse if the next line is header.
if (!_headerSyntax.pattern.hasMatch(nextLine)) {
return false;
}
Expand All @@ -740,13 +745,12 @@ class _HeaderWithAlignmentSyntax extends md.BlockSyntax {
md.Node? parse(md.BlockParser parser) {
final match = _alignmentNotationPattern.firstMatch(parser.current);

// We've parsed the alignment token on the current line. We know a paragraph starts on the
// next line. Move the parser to the next line so that we can parse the paragraph.
// We've parsed the alignment token on the current line. We know a header starts on the
// next line. Move the parser to the next line so that we can parse the header.
parser.advance();

final headerNode = _headerSyntax.parse(parser);

// Use markdown alignment converter from [_ParagraphWithAlignmentSyntax]
if (headerNode is md.Element) {
headerNode.attributes.addAll({'textAlign': _convertMarkdownAlignmentTokenToSuperEditorAlignment(match!.input)});
}
Expand Down
3 changes: 3 additions & 0 deletions super_editor_markdown/test/super_editor_markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ void main() {
// to serialize the alignment token.
expect(serializeDocumentToMarkdown(doc), '# Header1');
});

test('header with center alignment', () {
final doc = MutableDocument(nodes: [
ParagraphNode(
Expand All @@ -63,6 +64,7 @@ void main() {
]);
expect(serializeDocumentToMarkdown(doc), ':---:\n# Header1');
});

test('header with right alignment', () {
final doc = MutableDocument(nodes: [
ParagraphNode(
Expand All @@ -76,6 +78,7 @@ void main() {
]);
expect(serializeDocumentToMarkdown(doc), '---:\n# Header1');
});

test('header with justify alignment', () {
final doc = MutableDocument(nodes: [
ParagraphNode(
Expand Down

0 comments on commit b35e29c

Please sign in to comment.