# All files / src/lib/markdown-it/plugins/markdown-it-block-embed/services VideoServiceBase.js

10.81% Statements 4/37
0% Branches 0/28
12.5% Functions 1/8
10.81% Lines 4/37

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94

    126x
    126x
    126x

    21x
     // Copyright (c) Rotorz Limited and portions by original markdown-it-video authors
    // Licensed under the MIT license. See LICENSE file in the project root.

    "use strict";

    function defaultUrlFilter(url, _videoID, _serviceName, _options) {
      return url;
    }

    class VideoServiceBase {

      constructor(name, options, env) {
        this.name = name;
        this.options = Object.assign(this.getDefaultOptions(), options);
        this.env = env;
      }

      getDefaultOptions() {
        return {};
      }

      extractVideoID(reference) {
        return reference;
      }

      getVideoUrl(_videoID) {
        throw new Error("not implemented");
      }

      getFilteredVideoUrl(videoID) {
        let filterUrlDelegate = typeof this.env.options.filterUrl === "function"
            ? this.env.options.filterUrl
            : defaultUrlFilter;
        let videoUrl = this.getVideoUrl(videoID);
        return filterUrlDelegate(videoUrl, this.name, videoID, this.env.options);
      }

      getEmbedCode(videoID) {
        let containerClassNames = [];
        if (this.env.options.containerClassName && this.options.ignoreStyle !== true) {
          containerClassNames.push(this.env.options.containerClassName);
        }

        let escapedServiceName = this.env.md.utils.escapeHtml(this.name);
        containerClassNames.push(this.env.options.serviceClassPrefix + escapedServiceName);

        let containerStyles = [];
        if (this.options.ignoreStyle !== true) {
          containerStyles.push(["position: relative;"]);
          if (this.options.height !== undefined && this.options.width !== undefined) {
            containerStyles.push(`padding-bottom: ${this.options.height / this.options.width * 100}%`);
          }
        }

        let iframeAttributeList = [];
        iframeAttributeList.push([ "type", "text/html" ]);
        iframeAttributeList.push([ "src", this.getFilteredVideoUrl(videoID) ]);
        iframeAttributeList.push([ "frameborder", 0 ]);

        if (this.env.options.outputPlayerSize === true && this.options.ignoreStyle === true) {
          if (this.options.width !== undefined && this.options.width !== null) {
            iframeAttributeList.push([ "width", this.options.width ]);
          }
          if (this.options.height !== undefined && this.options.height !== null) {
            iframeAttributeList.push([ "height", this.options.height ]);
          }
        }

        if (this.env.options.allowFullScreen === true) {
          iframeAttributeList.push([ "webkitallowfullscreen" ]);
          iframeAttributeList.push([ "mozallowfullscreen" ]);
          iframeAttributeList.push([ "allowfullscreen" ]);
        }

        let iframeAttributes = iframeAttributeList
          .map(pair =>
            pair[1] !== undefined
                ? `${pair[0]}="${pair[1]}"`
                : pair[0]
          )
          .join(" ");

        return `<div class="${containerClassNames.join(" ")}" style="${containerStyles.join(" ")}">`
               + `<iframe ${iframeAttributes}></iframe>`
             + `</div>\n`;
      }

    }

    module.exports = VideoServiceBase;