Skip to main content

Formatting

buildfire.format#

This service give the ability to convert a plain text to a rich HTML, where it detects urls, emails, hashatgs and phone numbers and convert them to a clickable links (action items).

Requirements#

Widget#

Include format.js file in widget header after the buildfire.min.js

<head>
<script src="../../../scripts/buildfire/buildfire.min.js"></script>
<script src="../../../scripts/buildfire/services/format/format.js"></script>
</head>

Control#

Include format.js file in control header after the buildfire.min.js

<head>
<script src="../../../../scripts/buildfire/buildfire.min.js"></script>
<script src="../../../../scripts/buildfire/services/format/format.js"></script>
</head>

Methods#

textToHTML()
#

buildfire.format.textToHTML(options, callback)

This function is responsible for converting a plain text to a rich HTML, where it detects urls, emails, hashatgs and phone numbers and convert them to a clickable links (action items) and it also retruns the detected hashtags in the text as an array.

let options = {
text: 'Convert (https://www.google.com) to a clickable link'
};
buildfire.format.textToHTML(options, (err, result) => {
if(err) return console.error(`ERROR: ${err}`);
console.log("RESULT: " + result);
});

options#

NameTypeRequiredDescriptionDefault
textstringyesThe text that will be converted to html
ignoreAnchorsbooleannoto decide whether to convert urls to action items or to keep them as textsfalse
ignoreEmailsbooleannoto decide whether to convert emails to action items or to keep them as textsfalse
ignorePhoneNumbersbooleannoto decide whether to convert phone numbers to action items or to keep them as textsfalse
supportedHashtagTypestringnothe type of hashtag to be supported (youtube, facebook, twitter, custom)

callback(err, data)#

NameTypeDescription
errstringerror string, null when operation is successful
dataobjectcontains the converted text to html and the hashtags that was detected in the text as an array
Warning

This service checks if the text is already converted to an html; so if it is already converted, this service will retrun an error.

More Examples#

let options = {
text: 'Convert (https://www.google.com) to a clickable link and #youtubeHashtag to a clickable hashtag',
ignoreAnchors: false,
ignoreEmails: true,
ignorePhoneNumbers: false,
supportedHashtagType: 'youtube'
};
buildfire.format.textToHTML(options, (err, result) => {
if(err) return console.error(`ERROR: ${err}`);
console.log(result);
});

onHashtagClick()
#

buildfire.format.onHashtagClick(hashtag)

This function is used with the (supportedHashtagType = 'custom'), where this function will be invoked when the user clicks on the custom hashtag. Initially, this function logs the clicked hashtag in the console, but the developer can override this function to handle the clicked hashtag in different ways.

let options = {
text: 'Convert #customHashtag to a clickable hashtag',
supportedHashtagType: 'custom'
};
buildfire.format.textToHTML(options, (err, result) => {
if(err) return console.error(`ERROR: ${err}`);
console.log(result);
});
buildfire.format.onHashtagClick = function(hashtag) { // override the original function
console.log(`The overridden function was invoked with hashtag: ${hashtag}`);
}