انتقل إلى المحتوى الرئيسي
الوصفة رقم 218

دليل الوصفات · الفصل 13 · الدمج مع أنظمة إدارة المحتوى

تطبيق التعديلات المقبولة ونقل الملاحظات الأخرى معها

تكتب applyEdits التصحيحات المقبولة في Markdown وتنقل معها مرساة كل ملاحظة أخرى، وتعلّم الملاحظات التي وقع داخلها تعديل.

في هذه الصفحة
المستوى
متقدم
Postext
اختُبرت مع Postext 1.103.1
تتطلب ≥ 1.103.0
الترخيص
حُدّثت في 4 ديسمبر 2026
الشيفرة MIT · النص CC BY 4.0
المسودة والنص بعد applyEdits جنبًا إلى جنب: ثلاثة تعديلات مطبَّقة (أخضر)، وواحد مرفوض ما زال مفتوحًا (رمادي)، والملاحظة 2 معلَّمة 2? لأن تعديلًا وقع داخل كلماتها.
المسودة والنص بعد applyEdits جنبًا إلى جنب: ثلاثة تعديلات مطبَّقة (أخضر)، وواحد مرفوض ما زال مفتوحًا (رمادي)، والملاحظة 2 معلَّمة 2? لأن تعديلًا وقع داخل كلماتها.
  • نموذج باللغة الإنجليزية: لا توجد طبعة عربية بعد
  • مقاس القص 130 × 190 مم
  • عمود واحد
  • Spectral 9.4/13.4
  • Karla
  • صفحتان
  • المستوى
  • Postext 1.103.1
  • أُخرجت في 4 ms
  • أسطر الشيفرة: 153

باختصار

مقالة في مجلة عليها أربعة تصحيحات مقترحة وثلاث ملاحظات من محررة. تُقبل ثلاثة تصحيحات وتُكتب في النص. تنتقل الملاحظات مع كلماتها، والملاحظة التي تغيّرت كلماتها تُعلَّم لتُقرأ من جديد.

ما الذي ستنضده

مقالة من Field Notes on Ice، صفحة مجلة مقاسها 130 × 190 مم، تُعرض مرتين. على اليسار المسودة وعليها أربعة تعديلات مقترحة مرسومة بعلامات التصحيح وثلاث ملاحظات مرقمة من المحررة. وعلى اليمين النص بعد أن كُتبت التعديلات الثلاثة المقبولة في Markdown وأُعيد تنضيده: الكلمات الجديدة ملونة بالأخضر، والملاحظات منقولة مع كلماتها، والحذف المرفوض ما زال مفتوحًا بالرمادي، والملاحظة 2 معلَّمة بالكهرماني لأن تعديلًا مقبولًا غيّر الكلمات التي كانت عليها.

تجيب هذه الوصفة عن

  • كيف أطبّق التعديلات المقبولة على Markdown وأنقل الملاحظات الأخرى معها؟

الجواب المختصر

script.js · الأسطر 152–177في الشيفرة الكاملة
const accepted = edits.filter((e) => e.accepted);
const declined = edits.filter((e) => !e.accepted);
const kept = [...notes.map((n) => n.anchor), ...declined.map((e) => textAnchor(markdown, e))];
// One call: the new Markdown, a map of old offsets to new ones, and every anchor carried.
const { markdown: next, map, anchors } = applyEdits(markdown, accepted, kept);
const after = await buildDocumentWithFonts({ markdown: next }, config, kitFonts(FONTS));
const marks = [
  // The text each accepted edit put in, where it now is.
  ...accepted.map((e, i) => {
    const start = mapOffset(map, e.start, 'before');
    return { id: `a${i}`, kind: 'highlight', range: { start, end: start + e.text.length },
      color: '#1f8a4c' };
  }),
  // The notes: a note an edit fell inside is touched, for a person to read again.
  ...notes.map((n, i) => {
    const { anchor, touched } = anchors[i];
    return { id: `n${i}`, kind: 'note', range: anchor, label: touched ? `${i + 1}?` : `${i + 1}`,
      color: touched ? '#c4720a' : undefined };
  }),
  // The declined edit stays open on the new text.
  ...declined.map((e, k) => {
    const { start, end } = anchors[notes.length + k].anchor;
    return suggestion({ ...e, start, end }, `d${k}`);
  }),
];
openPages('after', after, next, marks);

المكونات

الخطوط
Spectral, Karla (SIL OFL 1.1)
الأصول
لا شيء: كل صورة مرسومة بالشيفرة

طريقة التحضير

#1 · احفظ المقترحات والملاحظات مراسيَ

script.js · الأسطر 94–125في الشيفرة الكاملة
// A suggestion: [words that place it, the words it changes ('' inserts after them), the new
// text ('' deletes), accepted?]. A note: [the words it is on, what the editor wrote].
const [EDITS, NOTES] = t({
  en: [[
    ['always on the move', 'on the move', 'moving', true],
    ['slides on it', '', ' (basal sliding)', true],
    ['the speed doubles', 'doubles', 'can double', true],
    ['like a very stiff', 'very ', '', false],
  ], [
    ['a stake hammered into the surface', 'A photograph of a survey stake?'],
    ['the speed doubles for a few days', 'Which glaciers? Give a source.'],
    ['between 30 and 200 metres a year', 'Name one glacier as an example.'],
  ]],
  es: [[
    ['siempre en movimiento', 'en movimiento', 'moviéndose', true],
    ['resbala sobre ella', '', ' (deslizamiento basal)', true],
    ['la velocidad se duplica', 'se duplica', 'puede duplicarse', true],
    ['líquido muy rígido', 'muy ', '', false],
  ], [
    ['una estaca clavada en la superficie', '¿Una foto de una estaca de medición?'],
    ['la velocidad se duplica durante unos días', '¿En qué glaciares? Falta la fuente.'],
    ['entre 30 y 200 metros al año', 'Pon un glaciar concreto como ejemplo.'],
  ]],
});
const place = (around, words) => {
  const at = markdown.indexOf(around) + (words ? around.indexOf(words) : around.length);
  return { start: at, end: at + words.length };
};
const edits = EDITS.map(([around, words, text, accepted]) =>
  ({ ...place(around, words), text, accepted }));
const notes = NOTES.map(([words, body]) =>
  ({ body, anchor: textAnchor(markdown, place(words, words)) }));

يضع الـ pen كل سجل على المسودة بحسب كلماته، كما يقرؤه نظام إدارة المحتوى من جداوله. يصير المقترح تعديلًا { start, end, text }: الطرفان المتساويان يعنيان إدراجًا، والنص الفارغ يعني حذفًا. وتحفظ الملاحظة textAnchor، أي مداها مع كلماته وشيئًا من السياق.

#2 · طبّق التعديلات المقبولة باستدعاء واحد

الشيفرة هي الجواب المختصر أعلاه. تأخذ applyEdits كل التعديلات بمواضع المسودة وبأي ترتيب، فلا يحتاج الموقع أبدًا إلى إزاحة تعديل بسبب آخر. وتعيد Markdown الجديد، وmap لأي موضع آخر، وكل مرساة منقولة عبر التعديلات: مزاحة بقدر ما أضافته التعديلات السابقة أو حذفته، وtouched إذا وقع تعديل داخلها. لا حاجة إلى بحث، فلا يمكن لمرساة نُقلت هكذا أن تصير ملتبسة. وتضع mapOffset مع 'before' كل نص جديد حيث يبدأ الآن.

#3 · بيّن معنى الألوان

script.js · الأسطر 181–190في الشيفرة الكاملة
const legend = t({
  en: [['Draft', '3 edits accepted, 1 declined (grey) · notes 1–3'],
    ['After applyEdits', 'green: applied · 2?: an edit fell inside note 2 · grey: still open']],
  es: [['Borrador', '3 cambios aceptados, 1 rechazado (gris) · notas 1–3'],
    ['Tras applyEdits', 'verde: aplicado · 2?: un cambio dentro de la nota 2 · gris: pendiente']],
});
for (const [i, [title, text]] of legend.entries()) {
  document.querySelectorAll('#pages h2')[i].textContent = title;
  document.querySelectorAll('#pages p')[i].textContent = text;
}

تحمل الملاحظة الملموسة علامة استفهام في رايتها ولونًا آخر. أما هل ما زالت الملاحظة تصلح لـ «can double» فتقرره المحررة؛ ولا يفعل Postext سوى الإبلاغ بأن الكلمات تغيّرت. والتعديلات المتداخلة ترمي RangeError، لأن تصحيحين للكلمات نفسها يحتاجان إلى من يختار بينهما.

الوصفة كاملة

// ═══ Postext Cookbook · Nº 218 · Applying accepted edits ═════════════════════════════
// https://postext.dev/en/cookbook/applying-accepted-edits
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Spectral, Karla (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// A magazine piece with four suggested edits and three editor's notes. Three edits are
// accepted and written into the Markdown with applyEdits; the notes and the declined edit are
// carried through as anchors, without a search, and a note an edit fell inside comes back
// flagged as touched. The draft and the new text are laid out side by side, each with its marks.
import { buildDocumentWithFonts, renderPageToCanvas } from 'https://esm.sh/postext';
import { textAnchor, applyEdits, mapOffset } from 'https://esm.sh/postext/annotate';
import { createViewer } from 'https://esm.sh/postext-viewer';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'applying-accepted-edits';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#1c1f24', // text
  accent: '#2b5f8a', // glacier blue: title, subheads, folios
  muted: '#677080', // running heads
};
const col = (id) => ({ hex: palette[id], model: 'hex', paletteId: id });
const colorPalette = [
  ...Object.entries(palette).map(([id, hex]) => ({ id, name: id, value: { hex, model: 'hex' } })),
  { id: 'main-color', name: 'accent (defaults)', value: { hex: palette.accent, model: 'hex' } },
];
// #endregion
const LEAD = 13.4; // pt of body leading

const head = (id, content, parity, edge, x, extra = {}) => ({ kind: 'text', id, content, parity,
  pages: 'body', fontFamily: 'Karla', fontSize: pt(7), fontWeight: 500, letterSpacing: pt(0.9),
  textTransform: 'uppercase', color: col('muted'),
  placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(10) } }, ...extra });
const folio = { fontWeight: 700, color: col('accent') };

const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,
  page: { sizePreset: 'custom', width: mm(130), height: mm(190), dpi: 150,
    margins: { top: mm(18), bottom: mm(18), left: mm(16), right: mm(14), mirror: true } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: 'Spectral', fontSize: pt(9.4), lineHeight: pt(LEAD),
    color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
    textAlign: 'justify', firstLineIndent: mm(4), indentAfterHeading: false,
    hyphenation: { enabled: true }, optimalLineBreaking: true,
    avoidWidows: true, avoidOrphans: true, avoidRunts: true },

يعمل ملف script.js المجمّع كما هو: الصقه في سكربت الوحدة (module) لأي صفحة، أو افتح الوصفة على CodePen. مجلد الوصفة على GitHub ↗ (يفتح في تبويب جديد)

تنويعات

#احفظ المراسي المنقولة

يكتب الموقع كل مرساة منقولة في صفها من جديد، لتبدأ المراجعة التالية من النص الجديد.

 const { markdown: next, map, anchors } = applyEdits(markdown, accepted, kept);
+notes.forEach((n, i) => { n.anchor = anchors[i].anchor; }); // write these back to the rows

الحقوق

الوصفة
Ignacio Ferro
النص
نص أصلي, CC BY 4.0
الخطوط
Spectral (SIL OFL 1.1) · Karla (SIL OFL 1.1)
الشيفرة
MIT، مثل Postext