Skip to content

Commit

Permalink
feat: 🎸 support custom markers other than using backtick
Browse files Browse the repository at this point in the history
This commit adds `remark-burger`, a custom parser that allow extracting
text between custom markers. It adds support for user-defined syntax
such as [[youtube: gdviJzEwShE]]

BREAKING CHANGE: 🧨 none
  • Loading branch information
d4rekanguok committed Apr 19, 2019
1 parent 4961952 commit 81e2299
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 12 deletions.
26 changes: 22 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
const config_1 = require("./src/config");
const EmbedVideo_1 = require("./src/EmbedVideo");
const remark_burger_1 = __importDefault(require("remark-burger"));
const visit = require(`unist-util-visit`);
const overrideDefaultOptions = (options) => {
const videoOptions = Object.assign({}, config_1.defaultOptions, options);
Expand All @@ -11,17 +15,31 @@ const overrideDefaultOptions = (options) => {
};
const addVideoIframe = ({ markdownAST }, options) => {
options = overrideDefaultOptions(options);
visit(markdownAST, `inlineCode`, (node) => {
const { value } = node;
const match = (node, v) => {
const keywords = [...config_1.knownPlatforms(), 'video'].join('|');
const re = new RegExp(`\(${keywords}\):\(\.\*\)`, 'i');
const processValue = value.match(re);
const processValue = v.match(re);
if (processValue) {
const type = processValue[1];
const id = processValue[2].trim();
node.type = `html`;
node.value = EmbedVideo_1.embedVideoHTML(type, id, options);
}
});
};
const { beginMarker, endMarker } = options;
if (beginMarker || endMarker) {
visit(markdownAST, `embedVideo`, (node) => {
const { data } = node;
match(node, data.content);
});
}
else {
visit(markdownAST, `inlineCode`, (node) => {
const { value } = node;
match(node, value);
});
}
};
const setParserPlugins = ({ beginMarker, endMarker }) => [[remark_burger_1.default, { beginMarker, endMarker, onlyRunWithMarker: true, pattyName: 'embedVideo' }]];
addVideoIframe.setParserPlugins = setParserPlugins;
module.exports = addVideoIframe;
27 changes: 21 additions & 6 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IEmbedVideoOptions } from "./src/interfaces";
import { IEmbedVideoOptions, Node } from "./src/interfaces";
import { defaultOptions, knownPlatforms } from "./src/config";
import { embedVideoHTML } from "./src/EmbedVideo";

import plugin from "remark-burger";

const visit = require(`unist-util-visit`);

Expand All @@ -18,21 +18,36 @@ const overrideDefaultOptions = (options: IEmbedVideoOptions): IEmbedVideoOptions
const addVideoIframe = ({ markdownAST }: any, options: IEmbedVideoOptions) => {
options = overrideDefaultOptions(options);

visit(markdownAST, `inlineCode`, (node: { type: string, value: string }) => {
const { value } = node;
const match = (node: Node, v: string): void => {
const keywords = [...knownPlatforms(), 'video'].join('|');
const re = new RegExp(`\(${keywords}\):\(\.\*\)`, 'i');

const processValue = value.match(re);
const processValue = v.match(re);
if (processValue) {
const type = processValue[1];
const id = processValue[2].trim();

node.type = `html`;
node.value = embedVideoHTML(type, id, options);
}
}

})
const { beginMarker, endMarker } = options;
if (beginMarker || endMarker) {
visit(markdownAST, `embedVideo`, (node: Node) => {
const { data } = node;
match(node, data.content);
})
} else {
visit(markdownAST, `inlineCode`, (node: Node) => {
const { value } = node;
match(node, value);
})
}
}

const setParserPlugins = ({ beginMarker, endMarker }: IEmbedVideoOptions) =>
[[ plugin, { beginMarker, endMarker, onlyRunWithMarker: true, pattyName: 'embedVideo' } ]]

addVideoIframe.setParserPlugins = setParserPlugins;
export = addVideoIframe;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dependencies": {
"@types/node": "^11.11.3",
"get-video-id": "^3.1.0",
"remark-burger": "^1.0.0",
"unist-util-visit": "^1.4.0"
},
"devDependencies": {
Expand Down
10 changes: 8 additions & 2 deletions src/interfaces.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { URL } from "url";
import { RemarkBurgerOptions } from "remark-burger";

interface IEmbedVideoOptions {
interface Node {
type: string;
value: string;
data: { content: string };
}

interface IEmbedVideoOptions extends RemarkBurgerOptions {
width: number;
ratio: number;
related?: boolean;
Expand All @@ -19,4 +26,3 @@ interface IVideoService {
urlProcessing?: (id: string, url: URL, options: IEmbedVideoOptions) => URL;
additionalHTML?: string;
}

5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ path-key@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"

remark-burger@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/remark-burger/-/remark-burger-1.0.0.tgz#6cddfe0cecea5c9a96ba2a2edc52f4e9e93daa61"
integrity sha512-5kmsrdSwajpC92puE52TWdQub1m3K/Phuwp0oyLieMDabbUUqM+/1TaukHx0KrOUwjSRYCvCeaW3ROCoX+xhmw==

right-pad@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/right-pad/-/right-pad-1.0.1.tgz#8ca08c2cbb5b55e74dafa96bf7fd1a27d568c8d0"
Expand Down

0 comments on commit 81e2299

Please sign in to comment.