Skip to content

Commit

Permalink
feat: add support for underline attribute with strict encoding parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
maelchiotti committed Sep 2, 2024
1 parent e430074 commit d0013aa
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 169 deletions.
1 change: 0 additions & 1 deletion packages/fleather/lib/fleather.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ export 'src/widgets/cursor.dart';
export 'src/widgets/editor.dart';
export 'src/widgets/editor_toolbar.dart';
export 'src/widgets/field.dart';
export 'src/widgets/fleather_localizations.dart';
export 'src/widgets/link.dart' show LinkActionPickerDelegate, LinkMenuAction;
export 'src/widgets/text_line.dart';
export 'src/widgets/theme.dart';
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.

148 changes: 0 additions & 148 deletions packages/fleather/lib/src/widgets/fleather_localizations.dart

This file was deleted.

66 changes: 46 additions & 20 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';

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

/// 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 _ParchmentMarkdownDecoder extends Converter<String, ParchmentDocument> {
}

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,8 +409,6 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
ParchmentAttribute? currentBlockAttribute;

void handleLine(LineNode node) {
if (node.hasBlockEmbed) return;

for (final attr in node.style.lineAttributes) {
if (attr.key == ParchmentAttribute.block.key) {
if (currentBlockAttribute != attr) {
Expand Down Expand Up @@ -486,8 +490,11 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
return buffer.toString();
}

void _writeAttribute(StringBuffer buffer, ParchmentAttribute? attribute,
{bool close = false}) {
void _writeAttribute(
StringBuffer buffer,
ParchmentAttribute? attribute, {
bool close = false,
}) {
if (attribute == ParchmentAttribute.bold) {
_writeBoldTag(buffer);
} else if (attribute == ParchmentAttribute.italic) {
Expand All @@ -497,18 +504,26 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
} else if (attribute == ParchmentAttribute.strikethrough) {
_writeStrikeThoughTag(buffer);
} else if (attribute?.key == ParchmentAttribute.link.key) {
_writeLinkTag(buffer, attribute as ParchmentAttribute<String>,
close: close);
_writeLinkTag(
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(
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) {
throw ArgumentError('Cannot handle $attribute');
}
}
Expand All @@ -521,6 +536,14 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
buffer.write('_');
}

void _writeUnderlineTag(StringBuffer buffer, {bool close = false}) {
if (false) {
buffer.write('</u>');
} else {
buffer.write('<u>');
}
}

void _writeInlineCodeTag(StringBuffer buffer) {
buffer.write('`');
}
Expand All @@ -529,8 +552,11 @@ class _ParchmentMarkdownEncoder extends Converter<ParchmentDocument, String> {
buffer.write('~~');
}

void _writeLinkTag(StringBuffer buffer, ParchmentAttribute<String> link,
{bool close = false}) {
void _writeLinkTag(
StringBuffer buffer,
ParchmentAttribute<String> link, {
bool close = false,
}) {
if (close) {
buffer.write('](${link.value})');
} else {
Expand Down
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 @@ final class LineNode extends ContainerNode<LeafNode> with StyledNode {
return false;
}

EmbedNode get embedNode {
assert(hasBlockEmbed);

return children.single as EmbedNode;
}

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

0 comments on commit d0013aa

Please sign in to comment.