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

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

تظليل انطلاقًا من قائمة نطاقات

سجلات أداة مراجعة، كلٌّ منها نطاق في Markdown وحكم، تُرسَم على الصفحات المُخرَجة تظليلًا بثلاثة ألوان باستخدام setAnnotations.

في هذه الصفحة
المُخرَج
Canvas
المستوى
أساسي
Postext
اختُبرت مع Postext 1.103.1
تتطلب ≥ 1.103.0
الترخيص
حُدّثت في 4 ديسمبر 2026
الشيفرة MIT · النص CC BY 4.0
خمسة ادعاءات في نشرة ملوّنة بحسب الحكم عليها: ما تُحقِّق منه بالأصفر، وما يحتاج إلى مصدر بالأزرق، وما يُسأل عنه الكاتب بالوردي، وكلٌّ منها مُدرج أيضًا بجانب الصفحات.
خمسة ادعاءات في نشرة ملوّنة بحسب الحكم عليها: ما تُحقِّق منه بالأصفر، وما يحتاج إلى مصدر بالأزرق، وما يُسأل عنه الكاتب بالوردي، وكلٌّ منها مُدرج أيضًا بجانب الصفحات.
  • نموذج باللغة الإنجليزية: لا توجد طبعة عربية بعد
  • مقاس القص 160 × 230 مم
  • عمود واحد
  • Source Serif 4 10.5/15
  • Source Sans 3
  • صفحة واحدة
  • المستوى
  • Postext 1.103.1
  • أُخرجت في 46 ms
  • أسطر الشيفرة: 102

باختصار

مقال في نشرة عن أشجار الدُّلب في لندن كما يراه مدقق الحقائق: العبارات المدقَّقة مظلَّلة بالأصفر أو الأزرق أو الوردي بحسب الحكم، وقائمة بجانب الصفحة تسمّيها. النقر على بند ينقلك إلى كلماته.

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

مقال في نشرة عن شجرة الدُّلب اللندنية، كما يراه قسم تدقيق الحقائق في أداة مراجعة. خمس عبارات تحمل حكمًا: الأصفر لما دُقِّق، والأزرق لما لا يزال الكاتب مدينًا بمصدره، والوردي لسؤال إلى الكاتب. يقع التظليل على الصفحة المُخرَجة، ويتجزأ حيث تنتقل العبارة إلى سطر جديد، والقائمة على اليمين تحمل السجلات الخمسة نفسها بترتيب القراءة. انقر بندًا فيأتي العارض بكلماته إلى مجال الرؤية.

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

  • كيف أرسم التظليل على الصفحات انطلاقًا من قائمة نطاقات في Markdown؟

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

script.js · الأسطر 112–128في الشيفرة الكاملة
const view = createViewer(document.getElementById('viewer'), {
  document: doc, markdown, zoom: 'fit-page', labels,
});
view.setAnnotations(claims.map(({ id, verdict, range }) => ({
  id, kind: 'highlight', range, color: VERDICTS[verdict].color, // 35 % over the text
})));
// The list beside the page: an entry brings its words into view.
const list = document.getElementById('claims');
for (const { id, verdict, range } of claims) {
  const item = document.createElement('button');
  item.style.setProperty('--tag', VERDICTS[verdict].color);
  item.innerHTML = '<b></b><span></span>';
  item.querySelector('b').textContent = VERDICTS[verdict].name;
  item.querySelector('span').textContent = markdown.slice(range.start, range.end);
  item.addEventListener('click', () => view.revealAnnotation(id, { behavior: 'smooth' }));
  list.append(item);
}

المكونات

الخطوط
Source Serif 4, Source Sans 3 (SIL OFL 1.1)
الأصول
لا شيء: كل صورة مرسومة بالشيفرة

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

#1 · احفظ السجلات نطاقاتٍ

script.js · الأسطر 85–103في الشيفرة الكاملة
// A CMS stores the offsets; the pen finds them from quotes so that both editions run.
const VERDICTS = {
  checked: { color: '#d39b00', name: t({ en: 'Checked', es: 'Comprobado' }) },
  source: { color: '#2f7fd6', name: t({ en: 'Needs a source', es: 'Falta la fuente' }) },
  query: { color: '#d64c86', name: t({ en: 'Query the author', es: 'Preguntar al autor' }) },
};
const claims = t({
  en: [['source', 'reached England from Virginia'],
    ['query', 'perhaps in Spain, perhaps in an English garden'], ['checked', 'set out in 1789'],
    ['source', 'it became the commonest street tree'],
    ['checked', 'first recorded in London in 2008']],
  es: [['source', 'llegó a Inglaterra desde Virginia'],
    ['query', 'quizá en España, quizá en un jardín inglés'], ['checked', 'plantados en 1789'],
    ['source', 'llegó a ser el árbol de calle más común'],
    ['checked', 'registrada por primera vez en Londres en 2008']],
}).map(([verdict, quote], i) => {
  const start = markdown.indexOf(quote);
  return { id: `c${i + 1}`, verdict, range: { start, end: start + quote.length } };
});

السجل هو ما تحفظه الأداة أصلًا: نطاق { start, end } في Markdown الفصل وحكم المراجِع. لا تعرف العلامات شيئًا عن الأحكام، والموقع هو الذي يجعل لكل حكم لونًا. الإزاحات هنا مأخوذة من اقتباس النص، لتعمل الطبعتان الإنجليزية والإسبانية؛ أما نظام إدارة المحتوى فيقرؤها من قاعدة بياناته.

#2 · ارسمها تظليلًا

الشيفرة هي الجواب المختصر أعلاه. تستبدل setAnnotations العلامات التي يعرضها العارض؛ وكل تظليل تعبئة شفافة فوق كل قطعة من نطاقه، بنسبة 35 % من color، فيبقى النص تحته أسود. استدعِها من جديد بالقائمة الجديدة متى تغيّرت السجلات، ويعيد العارض رسم الصفحات الظاهرة وحدها. القائمة على اليمين هي السجلات نفسها: تأتي revealAnnotation بالعلامة إلى مجال الرؤية بالمعرّف الذي أعطاها إياه الموقع.

#3 · سمِّ العلامات بلغة القارئ

script.js · الأسطر 107–108في الشيفرة الكاملة
const labels = t({ en: {}, es: { viewer: 'Páginas del boletín', page: 'Página {n} de {count}',
  kinds: { highlight: 'Resaltado' } } });

يجعل العارض كل علامة زرًا للوحة المفاتيح، اسمه مأخوذ من labels.kinds ومن النص المعلَّم، فتنطق قارئة الشاشة "Highlight: set out in 1789". تمرّر الطبعة الإسبانية أسماءها، ولا تحتاج الإنجليزية شيئًا.

الوصفة كاملة

// ═══ Postext Cookbook · Nº 213 · Highlights from a list of ranges ═══════════════════
// https://postext.dev/en/cookbook/highlights-from-ranges
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Source Serif 4, Source Sans 3 (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// A newsletter article on the London plane, as a fact-checker sees it in a review tool: the
// claims the desk has looked at are highlighted in three colours, one per verdict, and the
// list beside the page is the same records. A click on an entry brings its words into view.
import { buildDocumentWithFonts, renderPageToCanvas } from 'https://esm.sh/postext';
import { createViewer } from 'https://esm.sh/postext-viewer';

const LANG = 'en'; // @lang: the language of the sample document ('en' | 'es')
const RECIPE = 'highlights-from-ranges';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#22201c', // text
  accent: '#3d6b35', // leaf green: headings and folio
};
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

// #region page: a newsletter page, one wide column
const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,
  page: { sizePreset: 'custom', width: mm(160), height: mm(230), dpi: 150,
    margins: { top: mm(20), bottom: mm(24), left: mm(24), right: mm(24) } },
  layout: { layoutType: 'single' },
  bodyText: { fontFamily: 'Source Serif 4', fontSize: pt(10.5), lineHeight: pt(15),
    color: col('ink'), boldColor: col('ink'), italicColor: col('ink'), referenceColor: col('ink'),
    textAlign: 'justify', firstLineIndent: mm(4.5), indentAfterHeading: false,
    hyphenation: { enabled: true }, optimalLineBreaking: true,
    avoidWidows: true, avoidOrphans: true, avoidRunts: true },
  headings: { fontFamily: 'Source Sans 3', color: col('accent'), fontWeight: 700, levels: [
    { level: 1, fontSize: pt(26), lineHeight: pt(30), marginTop: pt(0), marginBottom: pt(15) },
    { level: 2, fontSize: pt(10.5), lineHeight: pt(15), marginTop: pt(15), marginBottom: pt(0) },
  ] },
  header: { elements: [] },
  footer: { elements: [
    { kind: 'text', id: 'folio', content: '{pageNumber}', fontFamily: 'Source Sans 3',
      fontSize: pt(8), fontWeight: 700, color: col('accent'), align: 'center',
      placement: { anchor: { to: 'page', edge: 'bottom-center' }, offset: { y: mm(-12) } } },

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

تنويعات

#اجعل التعبئة أقوى

على صفحة كريمية أو ملوّنة قد تبدو تعبئة 35 % باهتة.

-  document: doc, markdown, zoom: 'fit-page', labels,
+  document: doc, markdown, zoom: 'fit-page', labels, marks: { highlightOpacity: 0.5 },

#سطّر بدل التعبئة

لبروفة أهدأ، سطّر العبارات بلون الحكم.

-  id, kind: 'highlight', range, color: VERDICTS[verdict].color, // 35 % over the text
+  id, kind: 'underline', range, color: VERDICTS[verdict].color, // under the baseline

الحقوق

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