Skip to content

Commit 5f28aab

Browse files
author
Etienne Laurent
committed
clean
1 parent 1c2989d commit 5f28aab

1 file changed

Lines changed: 27 additions & 42 deletions

File tree

  • modules/@apostrophecms/rich-text-widget

modules/@apostrophecms/rich-text-widget/index.js

Lines changed: 27 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -937,8 +937,6 @@ module.exports = {
937937
// Quickly replaces inline image placeholder URLs with
938938
// actual, SEO-friendly URLs based on `widget._relatedDocs`.
939939
linkImages(widget, content) {
940-
console.log('----');
941-
console.log('content', content);
942940
// "Why no regexps?" We need to do this as quickly as we can.
943941
// indexOf and lastIndexOf are much faster.
944942
let i;
@@ -947,7 +945,6 @@ module.exports = {
947945
while (true) {
948946
const target = `${self.apos.modules['@apostrophecms/image'].action}/${doc.aposDocId}/src`;
949947
i = content.indexOf(target, offset);
950-
console.log('i', i);
951948
if (i === -1) {
952949
break;
953950
}
@@ -957,49 +954,37 @@ module.exports = {
957954
const left = content.lastIndexOf('<', i);
958955
const src = content.indexOf(' src="', left);
959956
const close = content.indexOf('"', src + 6);
960-
if ((left !== -1) && (src !== -1) && (close !== -1)) {
961-
content = content.substring(0, src + 5) + doc.attachment._urls[self.apos.modules['@apostrophecms/image'].getLargestSize()] + content.substring(close + 1);
962-
963-
// Update or insert alt attribute
964-
const tagEnd = content.indexOf('>', left);
965-
console.log('tagEnd', tagEnd);
966-
if (tagEnd !== -1) {
967-
let imgTag = content.substring(left, tagEnd + 1);
968-
console.log('imgTag', imgTag);
969-
const altAttr = ' alt="';
970-
const altIndex = imgTag.indexOf(altAttr);
971-
console.log('altIndex', altIndex);
972-
if (altIndex !== -1) {
973-
// Replace the existing alt value
974-
const altValueStart = altIndex + altAttr.length;
975-
console.log('altValueStart', altValueStart);
976-
const altValueEnd = imgTag.indexOf('"', altValueStart);
977-
console.log('altValueEnd', altValueEnd);
978-
imgTag = imgTag.substring(0, altValueStart) +
979-
self.apos.util.escapeHtml(doc.alt || '') +
980-
imgTag.substring(altValueEnd);
981-
console.log('updated imgTag', imgTag);
982-
} else {
983-
// Insert alt attribute before closing >
984-
imgTag = imgTag.replace(
985-
/\/?>$/,
986-
` alt="${self.apos.util.escapeHtml(doc.alt || '')}"$&`
987-
);
988-
console.log('inserted imgTag', imgTag);
989-
}
990-
// Replace the tag in content
991-
content = content.substring(0, left) +
992-
imgTag + content.substring(tagEnd + 1);
993-
994-
console.log('updated content', content);
995-
console.log('---');
996-
}
997957

998-
} else {
958+
if (left === -1 || src === -1 || close === -1) {
999959
// So we don't get stuck in an infinite loop
1000-
console.log('---');
1001960
break;
1002961
}
962+
963+
content = content.substring(0, src + 5) + doc.attachment._urls[self.apos.modules['@apostrophecms/image'].getLargestSize()] + content.substring(close + 1);
964+
965+
const tagEnd = content.indexOf('>', left);
966+
967+
if (tagEnd === -1) {
968+
continue;
969+
}
970+
971+
let imgTag = content.substring(left, tagEnd + 1);
972+
973+
const altAttr = ' alt="';
974+
const altIndex = imgTag.indexOf(altAttr);
975+
976+
if (altIndex === -1) {
977+
continue;
978+
}
979+
980+
const altValueStart = altIndex + altAttr.length;
981+
const altValueEnd = imgTag.indexOf('"', altValueStart);
982+
983+
// Replace the existing alt value in the img tag
984+
imgTag = imgTag.substring(0, altValueStart) + self.apos.util.escapeHtml(doc.alt || '') + imgTag.substring(altValueEnd);
985+
986+
// Replace the img tag in content
987+
content = content.substring(0, left) + imgTag + content.substring(tagEnd + 1);
1003988
}
1004989
}
1005990
return content;

0 commit comments

Comments
 (0)