@atlaskit/editor-wikimarkup-transformer
Version:
Wiki markup transformer for JIRA and Confluence
47 lines (46 loc) • 1.62 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
import { TokenType } from '../index';
import { isSafeUrl } from '@atlaskit/adf-schema';
import { parseString } from '../../text';
import { hasAnyOfMarks } from '../../utils/text';
export function urlLinkResolver(link, schema, context) {
var output = [];
var url = link.notLinkBody;
var textRepresentation = link.linkBody || link.notLinkBody;
if (!isSafeUrl(url)) {
return;
}
var ignoreTokenTypes = [TokenType.DOUBLE_DASH_SYMBOL, TokenType.TRIPLE_DASH_SYMBOL, TokenType.QUADRUPLE_DASH_SYMBOL, TokenType.LINK_TEXT, TokenType.ISSUE_KEY];
var rawContent = parseString({
ignoreTokenTypes: ignoreTokenTypes,
schema: schema,
context: context,
// Ignored via go/ees005
// eslint-disable-next-line require-unicode-regexp
input: textRepresentation.replace(/^mailto:/, '')
});
var decoratedContent = rawContent.map(function (n) {
var mark = schema.marks.link.create({
href: url
});
// We don't want to mix `code` mark with others
if (n.type.name === 'text' && !hasAnyOfMarks(n, ['link', 'code'])) {
return n.mark([].concat(_toConsumableArray(n.marks), [mark]));
}
return n;
});
output.push.apply(output, _toConsumableArray(decoratedContent));
if (!hasTextNode(rawContent)) {
var mark = schema.marks.link.create({
href: url
});
var linkTextNode = schema.text(textRepresentation, [mark]);
output.push(linkTextNode);
}
return output;
}
function hasTextNode(nodes) {
return nodes.find(function (n) {
return n.type.name === 'text';
});
}