176,200
עריכות
מהדורה קמא (שיחה | תרומות) אין תקציר עריכה |
מהדורה קמא (שיחה | תרומות) (הצעה לתיקון) |
||
שורה 28: | שורה 28: | ||
}); | }); | ||
function replaceDuplicateAnchors() { | |||
// קביעת משתנים והבניית הביטויים הרגולריים | // קביעת משתנים והבניית הביטויים הרגולריים | ||
var textArea = $('#wpTextbox1'); | var textArea = $('#wpTextbox1'); | ||
שורה 38: | שורה 38: | ||
// חיפוש והחלפת העוגנים הכפולים | // חיפוש והחלפת העוגנים הכפולים | ||
while ((match = anchorRegex.exec(content)) !== null) { | while ((match = anchorRegex.exec(content)) !== null) { | ||
var key = match[1].toLowerCase(); | |||
var text = match[2] !== undefined ? match[2] : key; // אם אין טקסט חופשי, השתמש בפרמטר העוגן | |||
if (!anchors[key]) { | |||
anchors[key] = []; | |||
} | |||
anchors[key].push({ index: match.index, length: match[0].length, text: text }); | |||
} | } | ||
// בדיקה שהתוכן של העוגנים בלבד השתנה | |||
var replacedContent = content; | |||
$.each(anchors, function (key, positions) { | $.each(anchors, function (key, positions) { | ||
if (positions.length > 1) { | |||
var offset = 0; | |||
var counter = 1; // מספר התוקף הנוכחי | |||
positions.forEach(function (pos, i) { | |||
var replacement; | |||
if (i > 0) { // מתחיל למספר מהעוגן השני | |||
counter++; | |||
replacement = `{{עוגן|${key}${counter}|${pos.text}}}`; | |||
} else { | |||
replacement = `{{עוגן|${key}|${pos.text}}}`; // העוגן הראשון ללא מספור | |||
} | |||
var startIndex = pos.index + offset; | |||
replacedContent = replacedContent.slice(0, startIndex) + replacement + replacedContent.slice(startIndex + pos.length); | |||
offset += replacement.length - pos.length; | |||
}); | |||
} | |||
}); | }); | ||
// בדיקה שהתוכן של התיבה שונה והודעה למשתמש | // בדיקה שהתוכן של התיבה שונה והודעה למשתמש | ||
if ( | if (replacedContent !== content) { | ||
var userContent = content.replace(/{{[^{}]+}}/g, ''); // הוצאת כל העוגנים מתוך התוכן המקורי | |||
var replacedUserContent = replacedContent.replace(/{{[^{}]+}}/g, ''); // הוצאת כל העוגנים מתוך התוכן שהוחלף | |||
if (userContent === replacedUserContent) { | |||
textArea.val(replacedContent); | |||
alert('הוחלפו עוגנים כפולים בהצלחה'); | |||
} else { | |||
alert('יש שינויים בתוכן מעבר לעוגנים ולכן לא ניתן להחליף את העוגנים.'); | |||
} | |||
} else { | } else { | ||
alert('אין עוגנים כפולים'); | |||
} | } | ||
} | |||
}); | }); | ||
}); | }); |