Merge pull request #356 from MidhunSureshR/linkify-doc

Add jsdoc comments for clickable link code + Minor Changes
This commit is contained in:
Bruno Windels 2021-05-18 13:30:23 +00:00 committed by GitHub
commit df8686099f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 1 deletions

View file

@ -1,5 +1,10 @@
import { linkify } from "./linkify/linkify.js"; import { linkify } from "./linkify/linkify.js";
/**
* Parse text into parts such as newline, links and text.
* @param {string} body A string to parse into parts
* @returns {MessageBody} Parsed result
*/
export function parsePlainBody(body) { export function parsePlainBody(body) {
const parts = []; const parts = [];
const lines = body.split("\n"); const lines = body.split("\n");
@ -16,7 +21,7 @@ export function parsePlainBody(body) {
for (let i = 0; i < lines.length; i += 1) { for (let i = 0; i < lines.length; i += 1) {
const line = lines[i]; const line = lines[i];
if (line.length) { if (line.length) {
linkify(lines[i], linkifyCallback); linkify(line, linkifyCallback);
} }
const isLastLine = i >= (lines.length - 1); const isLastLine = i >= (lines.length - 1);
if (!isLastLine) { if (!isLastLine) {

View file

@ -1,5 +1,13 @@
import { regex } from "./regex.js"; import { regex } from "./regex.js";
/**
* Splits text into links and non-links.
* For each such separated token, callback is called
* with the token and a boolean passed as argument.
* The boolean indicates whether the token is a link or not.
* @param {string} text Text to split
* @param {function(string, boolean)} callback A function to call with split tokens
*/
export function linkify(text, callback) { export function linkify(text, callback) {
const matches = text.matchAll(regex); const matches = text.matchAll(regex);
let curr = 0; let curr = 0;

View file

@ -28,6 +28,9 @@ export class TextMessageView extends TemplateView {
} }
} }
/**
* Map from part to function that outputs DOM for the part
*/
const formatFunction = { const formatFunction = {
text: textPart => text(textPart.text), text: textPart => text(textPart.text),
link: linkPart => tag.a({ href: linkPart.url, target: "_blank", rel: "noopener" }, [linkPart.text]), link: linkPart => tag.a({ href: linkPart.url, target: "_blank", rel: "noopener" }, [linkPart.text]),