Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ themes/default/public/lib/
*.pdf
.DS_Store
.idea

.env
74 changes: 49 additions & 25 deletions app/functions/contentProcessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import startCase from 'lodash/startCase.js';
import trim from 'lodash/trim.js';
import yaml from 'js-yaml';
import { error } from 'node:console';

Check warning on line 9 in app/functions/contentProcessors.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this unused import of 'error'.

See more on https://sonarcloud.io/project/issues?id=ryanlelek_Raneto&issues=AZ0lEbtM5aPA9hx_akRI&open=AZ0lEbtM5aPA9hx_akRI&pullRequest=410

// Regex for page meta (considers Byte Order Mark \uFEFF in case there's one)
// Look for the the following header formats at the beginning of the file:
Expand All @@ -17,8 +18,9 @@
// {header string}
// ---
// TODO: DEPRECATED Non-YAML
const META_REGEX = /^\uFEFF?\/\*([\s\S]*?)\*\//i;
const META_REGEX_YAML = /^\uFEFF?---([\s\S]*?)---/i;
const META_REGEX = /^\uFEFF?\/\*([\s\S]*)\*\//i;
const META_REGEX_YAML = /^\uFEFF?---\n([\s\S]*)\n---\n/i;
const META_REGEX_MARKDOWN_TITLE = /^\uFEFF?(---[\s\S]*---\n)?\# ([^\n]+)/i;

Check warning on line 23 in app/functions/contentProcessors.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Unnecessary escape character: \#.

See more on https://sonarcloud.io/project/issues?id=ryanlelek_Raneto&issues=AZ0lEbtM5aPA9hx_akRJ&open=AZ0lEbtM5aPA9hx_akRJ&pullRequest=410

function cleanString(str, useUnderscore = false) {
str = str.replaceAll('/', ' ').trim();
Expand Down Expand Up @@ -58,36 +60,54 @@

// Get metadata from Markdown content
function processMeta(markdownContent) {
if (META_REGEX.test(markdownContent)) {
const meta = {};
const metaArr = markdownContent.match(META_REGEX);
const metaString = metaArr?.[1]?.trim() ?? '';
let ret = {};
let metaString = '';
let meta = {};
let metaArr = {};

if (metaString) {
const lines = metaString.split('\n');
for (const line of lines) {
const colonIndex = line.indexOf(': ');
if (colonIndex <= 0) {
continue;
}
const key = line.substring(0, colonIndex).trim();
const value = line.substring(colonIndex + 2).trim();
if (key && value) {
meta[cleanString(key, true)] = value;
if (META_REGEX.test(markdownContent)) {
metaArr = markdownContent.match(META_REGEX);
if (metaArr !== null && metaArr[1] !== null) {
metaString = metaArr[1].trim();
if (metaString) {
const lines = metaString.split('\n');
for (const line of lines) {
const colonIndex = line.indexOf(': ');
if (colonIndex <= 0) {
continue;
}
const key = line.substring(0, colonIndex).trim();
const value = line.substring(colonIndex + 2).trim();
if (key && value) {
meta[cleanString(key, true)] = value;
}
}
}
ret = meta;
}
return meta;
}

try {
if (META_REGEX_YAML.test(markdownContent)) {
const metaArr = markdownContent.match(META_REGEX_YAML);
const metaString = metaArr?.[1]?.trim() ?? '';
const yamlObject = yaml.load(metaString);
return cleanObjectStrings(yamlObject);
// metaArr = markdownContent.match(META_REGEX_YAML);
// if (metaArr !== null && metaArr[1] !== null) {

Check warning on line 93 in app/functions/contentProcessors.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=ryanlelek_Raneto&issues=AZ0lEbtM5aPA9hx_akRK&open=AZ0lEbtM5aPA9hx_akRK&pullRequest=410
metaString = markdownContent.match(META_REGEX_YAML)[1].trim();
const yamlObject = yaml.load(metaString);
ret = cleanObjectStrings(yamlObject);
// }
}

return {};
} catch (e) {
console.error(e);
}
try {
if (!("Title" in ret) && META_REGEX_MARKDOWN_TITLE.test(markdownContent)) {
const title = markdownContent.match(META_REGEX_MARKDOWN_TITLE)[2];
ret.title = title;
}
} catch (e) {
console.error(e);
}
return ret;
}

// Replace content variables in Markdown content
Expand Down Expand Up @@ -121,7 +141,11 @@
const title = meta.title ? meta.title : slugToTitle(id);
const body = file;

return { id, title, body };
return {
id,
title,
body
};
} catch (e) {
if (debug) {
console.log(e);
Expand Down
33 changes: 17 additions & 16 deletions app/functions/createMetaInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,25 @@

// Returns an empty string if all input strings are empty
function createMetaInfo(metaTitle, metaDescription, metaSort) {
const yamlDocument = {};
const metaInfoIsPresent = metaTitle || metaDescription || metaSort;
const yamlDocument = {};
const metaInfoIsPresent = metaTitle || metaDescription || metaSort;

if (metaInfoIsPresent) {
if (metaTitle) {
yamlDocument.Title = metaTitle;
}
if (metaDescription) {
yamlDocument.Description = metaDescription;
}
if (metaSort) {
yamlDocument.Sort = Number.parseInt(metaSort, 10);
}
if (metaInfoIsPresent) {
if (metaTitle) {
yamlDocument.Title = metaTitle;
}
if (metaDescription) {
yamlDocument.Description = metaDescription;
}
if (metaSort) {
yamlDocument.Sort = Number.parseInt(metaSort, 10);
}

return `---\n${yaml.dump(yamlDocument)}---\n`;
}
return '---\n---\n';
return `---\n${yaml.dump(yamlDocument)}---\n`;
}
return '---\n---\n';
// return '';

Check warning on line 23 in app/functions/createMetaInfo.js

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Remove this commented out code.

See more on https://sonarcloud.io/project/issues?id=ryanlelek_Raneto&issues=AZ0lEbsy5aPA9hx_akRH&open=AZ0lEbsy5aPA9hx_akRH&pullRequest=410
}

// Exports
export default createMetaInfo;
export default createMetaInfo;
Loading