zefyr icon indicating copy to clipboard operation
zefyr copied to clipboard

EmbedAttribute in `notusMarkdown.encode()`

Open sil-main-coo opened this issue 5 years ago • 6 comments

Why can't I encode images?

My code: print('Body:'+ notusMarkdown.encode(_delta).toString());

And error:

The following ArgumentError was thrown while handling a gesture:
Invalid argument(s): Cannot handle embed: {type: image, source: file:///storage/emulated/0/Android/data/.../files/Pictures/abc.jpg}

When the exception was thrown, this was the stack: 
#0      _NotusMarkdownEncoder._writeAttribute (package:notus/src/convert/markdown.dart:172:7)
#1      _NotusMarkdownEncoder._writeInline (package:notus/src/convert/markdown.dart:152:7)
#2      _NotusMarkdownEncoder.convert._handleSpan (package:notus/src/convert/markdown.dart:65:11)
#3      _NotusMarkdownEncoder.convert (package:notus/src/convert/markdown.dart:87:20)
#4      Codec.encode (dart:convert/codec.dart:21:32)
...
Handler: "onTap"
Recognizer: TapGestureRecognizer#37a81
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(358.5, 57.0)
  finalLocalPosition: Offset(22.5, 32.0)
  sent tap down```


sil-main-coo avatar Sep 13 '19 07:09 sil-main-coo

In markdow.dart file, I added a conditional statement at _writeAttribute function , like this:

void _writeAttribute(StringBuffer buffer, NotusAttribute attribute,
      {bool close: false}) {
    if (attribute == NotusAttribute.bold) {
      _writeBoldTag(buffer);
    } else if (attribute == NotusAttribute.italic) {
      _writeItalicTag(buffer);
    } else if (attribute.key == NotusAttribute.link.key) {
      _writeLinkTag(buffer, attribute as NotusAttribute<String>, close: close);
    } else if (attribute.key == NotusAttribute.heading.key) {
      _writeHeadingTag(buffer, attribute as NotusAttribute<int>);
    } else if (attribute.key == NotusAttribute.block.key) {
      _writeBlockTag(buffer, attribute as NotusAttribute<String> , close: close);
    } else if (attribute.key == NotusAttribute.embed.key) {
      _writeImageTag(buffer, attribute as NotusAttribute<Map<String, dynamic>>, close: close);
    } else {
      throw new ArgumentError('Cannot handle $attribute');
    }
  }

And create _writeImageTag function:

void _writeImageTag(StringBuffer buffer, NotusAttribute<Map<String, dynamic>> string,
      {bool close: false}) {
    print('handling <<<<');
    print(string.value['source']);
    if (close) {
      buffer.write(']('+string.value['source']+')');
    } else {
      buffer.write('![Image');
    }
  }

It worked for me !

sil-main-coo avatar Sep 13 '19 08:09 sil-main-coo

Thanks for the research and yes, your solution is almost correct with one exception that there is two types of embeds: images and horizontal rule. So you need to update _writeImageTag to handle horizontal rules (and maybe rename that method to _writeEmbed).

Also, feel free to open a pull request with this change, I'd be happy to merge it in and release.

pulyaevskiy avatar Sep 14 '19 22:09 pulyaevskiy

@sil-main-coo Horizontal line is also a "NotusAttribute.embed.key", your method cannot work for it.

woodstream avatar Mar 06 '20 08:03 woodstream

可是这样子的转换只是把本地的数据放进去而已,不能转换为网络上的uri_image??? But this kind of conversion just puts local data in, not into the uri_image on the network. image

shaoting0730 avatar Nov 19 '20 04:11 shaoting0730

可是这样子的转换只是把本地的数据放进去而已,不能转换为网络上的uri_image??? But this kind of conversion just puts local data in, not into the uri_image on the network. image

你需要在 picker_image 中上传图片到服务器

aboutmydreams avatar Feb 17 '21 02:02 aboutmydreams

感谢,我将多位大佬解决的问题合并入代码然后后放在了仓库,并将 notus 和 zefyr 做了拆分,有需要的可以这样使用

dependency_overrides:
  notus:
    git: https://github.com/aboutmydreams/notus.git
  zefyr: 
    git: https://github.com/aboutmydreams/zefyr.git

aboutmydreams avatar Feb 17 '21 02:02 aboutmydreams