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

Handle unsupported attributes in Markdown conversion + horizontal lines #418

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions packages/fleather/lib/l10n/fleather_localizations.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/fleather/lib/l10n/fleather_localizations_en.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/fleather/lib/l10n/fleather_localizations_fa.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/fleather/lib/l10n/fleather_localizations_fr.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions packages/fleather/lib/src/widgets/theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,18 @@
}

/// The data from the closest [FleatherTheme] instance that encloses the given
/// context.
/// [context].
///
/// Returns `null` if there is no [FleatherTheme] in the given build context
/// and [nullOk] is set to `true`. If [nullOk] is set to `false` (default)
/// then this method asserts.
static FleatherThemeData? of(BuildContext context, {bool nullOk = false}) {
final widget = context.dependOnInheritedWidgetOfExactType<FleatherTheme>();
if (widget == null && nullOk) return null;
assert(widget != null,
'$FleatherTheme.of() called with a context that does not contain a FleatherTheme.');
assert(
widget != null,
'${FleatherTheme.of} called with a context that does not contain a $FleatherTheme.',

Check warning on line 42 in packages/fleather/lib/src/widgets/theme.dart

View check run for this annotation

Codecov / codecov/patch

packages/fleather/lib/src/widgets/theme.dart#L42

Added line #L42 was not covered by tests
);
return widget!.data;
}
}
Expand Down
76 changes: 57 additions & 19 deletions packages/parchment/lib/src/codecs/markdown.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import 'dart:convert';

import 'package:parchment_delta/parchment_delta.dart';

import '../document.dart';
import '../document/attributes.dart';
import '../document/block.dart';
import '../document/leaf.dart';
import '../document/line.dart';
import 'package:parchment/parchment.dart';
maelchiotti marked this conversation as resolved.
Show resolved Hide resolved

class ParchmentMarkdownCodec extends Codec<ParchmentDocument, String> {
const ParchmentMarkdownCodec();
const ParchmentMarkdownCodec({this.strictEncoding = true});

maelchiotti marked this conversation as resolved.
Show resolved Hide resolved
/// Whether to strictly stick to the Markdown syntax during the encoding.
///
/// If this option is enabled, during the encoding, if attributes that are
/// not natively supported by the Markdown syntax exist, an exception will be
/// thrown. Otherwise, they will be converted in the best way possible
/// (for example with HTML tags, plain text or placeholders).
final bool strictEncoding;

@override
Converter<String, ParchmentDocument> get decoder =>
_ParchmentMarkdownDecoder();

@override
Converter<ParchmentDocument, String> get encoder =>
_ParchmentMarkdownEncoder();
_ParchmentMarkdownEncoder(strict: strictEncoding);
}

class _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
Expand Down Expand Up @@ -354,6 +356,10 @@
}

class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
const _ParchmentMarkdownEncoder({required this.strict});

final bool strict;

static final simpleBlocks = <ParchmentAttribute, String>{
ParchmentAttribute.bq: '> ',
ParchmentAttribute.ul: '* ',
Expand Down Expand Up @@ -403,7 +409,13 @@
ParchmentAttribute? currentBlockAttribute;

void handleLine(LineNode node) {
if (node.hasBlockEmbed) return;
if (node.hasBlockEmbed) {
if (node.embedNode.value == BlockEmbed.horizontalRule) {
_writeHorizontalLineTag(buffer);

Check warning on line 414 in packages/parchment/lib/src/codecs/markdown.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/codecs/markdown.dart#L413-L414

Added lines #L413 - L414 were not covered by tests
}

return;
}

for (final attr in node.style.lineAttributes) {
if (attr.key == ParchmentAttribute.block.key) {
Expand Down Expand Up @@ -486,8 +498,11 @@
return buffer.toString();
}

void _writeAttribute(StringBuffer buffer, ParchmentAttribute? attribute,
{bool close = false}) {
void _writeAttribute(
maelchiotti marked this conversation as resolved.
Show resolved Hide resolved
StringBuffer buffer,
ParchmentAttribute? attribute, {
bool close = false,
}) {
if (attribute == ParchmentAttribute.bold) {
_writeBoldTag(buffer);
} else if (attribute == ParchmentAttribute.italic) {
Expand All @@ -497,18 +512,26 @@
} else if (attribute == ParchmentAttribute.strikethrough) {
_writeStrikeThoughTag(buffer);
} else if (attribute?.key == ParchmentAttribute.link.key) {
_writeLinkTag(buffer, attribute as ParchmentAttribute<String>,
close: close);
_writeLinkTag(
maelchiotti marked this conversation as resolved.
Show resolved Hide resolved
buffer,
attribute as ParchmentAttribute<String>,
close: close,
);
} else if (attribute?.key == ParchmentAttribute.heading.key) {
_writeHeadingTag(buffer, attribute as ParchmentAttribute<int>);
} else if (attribute?.key == ParchmentAttribute.block.key) {
_writeBlockTag(buffer, attribute as ParchmentAttribute<String>,
close: close);
_writeBlockTag(
maelchiotti marked this conversation as resolved.
Show resolved Hide resolved
buffer,
attribute as ParchmentAttribute<String>,
close: close,
);
} else if (attribute?.key == ParchmentAttribute.checked.key) {
// no-op already handled in handleBlock
} else if (attribute?.key == ParchmentAttribute.indent.key) {
// no-op already handled in handleBlock
} else {
} else if (!strict && attribute?.key == ParchmentAttribute.underline.key) {
_writeUnderlineTag(buffer, close: close);
} else if (strict) {

Check warning on line 534 in packages/parchment/lib/src/codecs/markdown.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/codecs/markdown.dart#L532-L534

Added lines #L532 - L534 were not covered by tests
throw ArgumentError('Cannot handle $attribute');
}
}
Expand All @@ -521,6 +544,14 @@
buffer.write('_');
}

void _writeUnderlineTag(StringBuffer buffer, {bool close = false}) {

Check warning on line 547 in packages/parchment/lib/src/codecs/markdown.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/codecs/markdown.dart#L547

Added line #L547 was not covered by tests
if (close) {
buffer.write('</u>');

Check warning on line 549 in packages/parchment/lib/src/codecs/markdown.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/codecs/markdown.dart#L549

Added line #L549 was not covered by tests
} else {
buffer.write('<u>');

Check warning on line 551 in packages/parchment/lib/src/codecs/markdown.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/codecs/markdown.dart#L551

Added line #L551 was not covered by tests
}
}

void _writeInlineCodeTag(StringBuffer buffer) {
buffer.write('`');
}
Expand All @@ -529,8 +560,11 @@
buffer.write('~~');
}

void _writeLinkTag(StringBuffer buffer, ParchmentAttribute<String> link,
{bool close = false}) {
void _writeLinkTag(
maelchiotti marked this conversation as resolved.
Show resolved Hide resolved
StringBuffer buffer,
ParchmentAttribute<String> link, {
bool close = false,
}) {
if (close) {
buffer.write('](${link.value})');
} else {
Expand Down Expand Up @@ -560,4 +594,8 @@
}
}
}

void _writeHorizontalLineTag(StringBuffer buffer) {
buffer.write('---');

Check warning on line 599 in packages/parchment/lib/src/codecs/markdown.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/codecs/markdown.dart#L598-L599

Added lines #L598 - L599 were not covered by tests
}
}
6 changes: 6 additions & 0 deletions packages/parchment/lib/src/document/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
return false;
}

EmbedNode get embedNode {
maelchiotti marked this conversation as resolved.
Show resolved Hide resolved
assert(hasBlockEmbed);

Check warning on line 30 in packages/parchment/lib/src/document/line.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/line.dart#L29-L30

Added lines #L29 - L30 were not covered by tests

return children.single as EmbedNode;

Check warning on line 32 in packages/parchment/lib/src/document/line.dart

View check run for this annotation

Codecov / codecov/patch

packages/parchment/lib/src/document/line.dart#L32

Added line #L32 was not covered by tests
}

/// Returns next [LineNode] or `null` if this is the last line in the document.
LineNode? get nextLine {
if (isLast) {
Expand Down
Loading