commit
ab4d3dacdf
File diff suppressed because one or more lines are too long
|
|
@ -10,26 +10,69 @@ const Article = ({article}) => {
|
||||||
|
|
||||||
let output = [""]; // Stores all text in a list
|
let output = [""]; // Stores all text in a list
|
||||||
let loc = 0; // Stores the current location in output that we're working with
|
let loc = 0; // Stores the current location in output that we're working with
|
||||||
|
let tmp;
|
||||||
for (let i = 0; i < text.length; i++) { // Iterate through the entire text string
|
let i = 0;
|
||||||
|
console.log(text);
|
||||||
|
while (i < text.length) { // Iterate through the entire text string
|
||||||
if (text.slice(i, i+4) === "http"){ // slice from i to 4 chars plus and check for http
|
if (text.slice(i, i+4) === "http"){ // slice from i to 4 chars plus and check for http
|
||||||
let x = i; // store i in x so the location is not modified
|
let x = i; // store i in x so the location is not modified
|
||||||
|
|
||||||
for (i; text[i] !== " " && i < text.length; i++){} // iterate until we find the end of the link denoted by a space
|
for (let y = i; ![" ", "\n"].includes(text[y]) && y < text.length; y++){
|
||||||
|
i=y;
|
||||||
|
} // iterate until we find the end of the link denoted by a space
|
||||||
|
|
||||||
if (output[loc] !== "") { // if the current output location isn't empty, increment loc
|
if (output[loc] !== "") { // if the current output location isn't empty, increment loc
|
||||||
loc++;
|
loc++;
|
||||||
}
|
}
|
||||||
|
tmp = text.slice(x, i+1);
|
||||||
// Put anchor for link into output list
|
// Put anchor for link into output list
|
||||||
output[loc] = <a key={i} href={text.slice(x, i)}>{text.slice(x, i)}</a>;
|
output[loc] = <a key={i} href={tmp}>{tmp}</a>;
|
||||||
output[++loc] = ""; // Create new location in output with empty string
|
output[++loc] = ""; // Create new location in output with empty string
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Append current char to output
|
// Append current char to output
|
||||||
output[loc] += text[i];
|
output[loc] += text[i];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
i++;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the output
|
||||||
|
return output;
|
||||||
|
};
|
||||||
|
|
||||||
|
let newLineProcessor = (text) => {
|
||||||
|
/**
|
||||||
|
* Given some text, processes and returns jsx with line breaks
|
||||||
|
*/
|
||||||
|
let tmp;
|
||||||
|
let output = [""]; // Stores all text in a list
|
||||||
|
let loc = 0; // Stores the current location in output that we're working with
|
||||||
|
for (let i = 0; i < text.length; i++) { // Iterate through the entire text string
|
||||||
|
if (!React.isValidElement(text[i])) {
|
||||||
|
tmp = [""];
|
||||||
|
loc = 0;
|
||||||
|
for (let j = 0; j < text[i].length; j++){
|
||||||
|
|
||||||
|
if (text[i].slice(j, j+1) === "\n"){ // slice from i to 4 chars plus and check for http
|
||||||
|
if (tmp[loc] !== "") { // if the current output location isn't empty, increment loc
|
||||||
|
loc++;
|
||||||
|
}
|
||||||
|
tmp[loc] = <br key={i+j}/>;
|
||||||
|
tmp[++loc] = "";
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// Append current char to output
|
||||||
|
tmp[loc] += text[i][j];
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
tmp[++loc] = "";
|
||||||
|
} else {
|
||||||
|
tmp = text[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
output.push(tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the output
|
// Return the output
|
||||||
|
|
@ -37,14 +80,14 @@ const Article = ({article}) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
let articleFormatter = (text) => {
|
let articleFormatter = (text) => {
|
||||||
let output = []; // Used to store separate formatted text
|
let output = [""]; // Used to store separate formatted text
|
||||||
let type = []; // Parallel to output list to signify format type
|
let type = []; // Parallel to output list to signify format type
|
||||||
let ind = 0; // Denote index of output
|
let ind = 0; // Denote index of output
|
||||||
let tick=false; // used to check if we're currently in formatted text.
|
let tick=false; // used to check if we're currently in formatted text.
|
||||||
let delimiters = ['', '`', '*', '~']; // Denotes characters used to format
|
let delimiters = ['', '`', '*', '~']; // Denotes characters used to format
|
||||||
|
|
||||||
for (let i = 0; i < text.length; i++) { // Iterate through input
|
for (let i = 0; i < text.length; i++) { // Iterate through input
|
||||||
if (delimiters.indexOf(text[i]) !== -1) { // Detect Code Delimiter
|
if (delimiters.indexOf(text[i]) >= 0) { // Detect Code Delimiter
|
||||||
|
|
||||||
if (tick) { // Close the code section
|
if (tick) { // Close the code section
|
||||||
output[++ind] = ""
|
output[++ind] = ""
|
||||||
|
|
@ -53,7 +96,7 @@ const Article = ({article}) => {
|
||||||
} else { // Start a new code section
|
} else { // Start a new code section
|
||||||
type.push(delimiters.indexOf(text[i]));
|
type.push(delimiters.indexOf(text[i]));
|
||||||
|
|
||||||
if (!output[ind]) {
|
if (output.length < ind) {
|
||||||
|
|
||||||
output[ind] = "";
|
output[ind] = "";
|
||||||
|
|
||||||
|
|
@ -70,20 +113,20 @@ const Article = ({article}) => {
|
||||||
if (output.length > type.length) { // If this is the beggining of a default text type
|
if (output.length > type.length) { // If this is the beggining of a default text type
|
||||||
|
|
||||||
type.push(0);
|
type.push(0);
|
||||||
output[ind] = ""
|
//output[ind] = text[i]
|
||||||
|
|
||||||
|
} else if (output.length < type.length) {
|
||||||
|
output[++ind] = ""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
output[ind] += text[i]
|
output[ind] += text[i]
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return [...output.keys()].map((i)=>{ // Format text and return as jsx
|
return [...output.keys()].map((i)=>{ // Format text and return as jsx
|
||||||
|
|
||||||
let text = linkProcessor(output[i]); // Process links
|
let text = linkProcessor(output[i]); // Process links
|
||||||
|
text = newLineProcessor(text);
|
||||||
if (type[i] === 0){ // Return default text type
|
if (type[i] === 0){ // Return default text type
|
||||||
|
|
||||||
return <div key={i}>{text}</div>;
|
return <div key={i}>{text}</div>;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue