How to use BinaryParser for my custom Parser if a condition is false

What’s the end goal?

I am trying to add a parser module for docx files. If I fix the problem then I will open a PR in the official repository.

What have I done so far?

(function () {
  /*jslint node: true, browser: true */
  /*global $tw: false */
  "use strict";

  const BinaryParser = require("./binaryparser");

  var DocxParser = function (type, text, options) {
    var element_uri = {
        type: "element",
        tag: "iframe",
        attributes: {
          src: {
            type: "string",
            value: `https://view.officeapps.live.com/op/embed.aspx?src=${options._canonical_uri}`,
          },
          loading: { type: "string", value: "lazy" },
          style: {
            type: "string",
            value: "border:0; width: 100%; object-fit: contain",
          },
        },
      },
      src;

    this.tree = options._canonical_uri ? [element_uri] : [];
  };

  exports[
    "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
  ] = DocxParser;
})();

This parser works fine if following conditions are met

  1. A tiddler type is application/vnd.openxmlformats-officedocument.wordprocessingml.document
  2. Tiddler has _canonical_uri field set

In this case, the MS Office live viewer shows the document in an iframe.

What’s the challenge?

If a user drops a docx file to TW, then it gets added as a binary data. In that case the TiddlyWIki SHOULD SHOW binary data warning.

How do I tell my parser to use BinaryParser if options.__canonical_uri field is not set?

I suspect, I need to do something like this

    this.tree = options._canonical_uri ? [element_uri] : [BinaryParser.element];

But I am not sure about the correct syntax.

What needs to be done here is that if a tiddler does not have _canonical_uri, then TiddlyWiki should use BinaryParser instead of DocxParser.

But I am not sure how to do it, because I am not very familiar with TW source code.


Another solution is to have two elements.

  1. If _canonical_uri is defined then use iframe element

  2. If _canonical_uri is not defined, then use div element and show a warning.

It is easier but this way we lose the benefit of all the translations and features that BinaryParses offer.

I will admit that I don’t quite follow the use case, however you could just try reusing the parts of the Binary Parser code that you need: https://github.com/Jermolene/TiddlyWiki5/blob/master/core/modules/parsers/binaryparser.js

Or try something along these lines:
this.tree = options._canonical_uri ? [element_uri] : BinaryParser.apply(this,arguments).tree;

Edit:

this.tree = options._canonical_uri ? [element_uri] : (new BinaryParser(type,text,options)).tree;

Thank you @saqimtiaz

The only issue is that TiddlyWIki will end up having duplicate code. In fact, I will have to copy BinaryParser completely.

Thanks for the hint @saqimtiaz

I tried different variations like the following

Incorrect code snippet
    this.tree = options._canonical_uri
      ? [element_uri]
      : Object.create(BinaryParser, {
          type: {
            value: type,
            enumerable: false,
            writable: true,
            configurable: true,
          },
          text: {
            value: text,
            enumerable: false,
            writable: true,
            configurable: true,
          },
          options: {
            value: { ...options },
            enumerable: false,
            writable: true,
            configurable: true,
          },
        }).tree;

In the end, I realized my approach is wrong :x: .

I exported variables from the BinaryParser and reused them inside my DocxParser.

I have opened a PR in TiddlyWIki repository

If you can spare sometime then please do review.

Hope the project maintainers will deem it worthy to merge.