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

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

انقر على الصفحة لتجد موضعها في النص الأصلي

تحوّل sourceAt وcaretAt نقرةً على صفحة منضَّدة إلى كتلة من Markdown وموضع فيه؛ ويعرض نظام إدارة المحتوى ذلك السطر من الأصل معلَّمًا.

في هذه الصفحة
المستوى
متوسط
Postext
اختُبرت مع Postext 1.103.1
تتطلب ≥ 1.103.0
الترخيص
حُدّثت في 4 ديسمبر 2026
الشيفرة MIT · النص CC BY 4.0
نقرة على «a large correction» في الصفحة 1: كتلتها ملوّنة على الصفحة، ومؤشر يعلّم الإزاحة، والمصدر بجانبها يعرض الكتلة نفسها والسلسلة text › line › block.
نقرة على «a large correction» في الصفحة 1: كتلتها ملوّنة على الصفحة، ومؤشر يعلّم الإزاحة، والمصدر بجانبها يعرض الكتلة نفسها والسلسلة text › line › block.
  • نموذج باللغة الإنجليزية: لا توجد طبعة عربية بعد
  • مقاس القص 148 × 210 مم
  • عمود واحد
  • Source Serif 4 9.5/13.5
  • IBM Plex Sans
  • صفحتان
  • المستوى
  • Postext 1.103.1
  • أُخرجت في 78 ms
  • أسطر الشيفرة: 145

باختصار

فصل من دليل لإصلاح الدراجات معروض صفحاتٍ، وبجانبه النص الذي خرجت منه. نقرة على كلمة في الصفحة تجد الكلمة نفسها في النص، وتعلّم فقرتها، وتذكر رقم سطرها.

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

فصل من The Home Bicycle Workshop على صفحة 148 × 210 مم، معروض في postext-viewer على اليسار، ونص Markdown الخاص به على اليمين كما يحفظه نظام تحرير. نقرة في أي موضع من الصفحة تنقل لوحة الأصل إلى السطر الذي تحت المؤشر: تتلوّن الفقرة في الجانبين، ويقف مؤشر أخضر عند الحرف، ويذكر سطرا الحالة السطر والعمود والموضع، ثم سلسلة ما هو مرسوم في تلك النقطة. تُظهر البطاقة نقرة جاهزة في وسط فقرة من القسم الثالث.

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

  • كيف أعرف أيّ جزء من Markdown وقعت عليه نقرة على الصفحة؟

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

script.js · الأسطر 122–145في الشيفرة الكاملة
const span = ({ level, source: s }) => `${level} ${s.kind === 'config' ? s.path
  : s.start === undefined ? s.resourceId : `[${s.start}, ${s.end})`}`;
function pick(pageIndex, x, y) {
  // What is painted there, innermost first: a glyph, its line, its paragraph, a box round
  // it; a running head answers with the config entry that set it.
  const chain = sourceAt(doc, pageIndex, x, y);
  $('chain').textContent = chain.map(span).join(' › ');
  const block = chain.find((e) => e.level === 'block' && e.source.kind === 'markdown')?.source;
  const hit = caretAt(doc, pageIndex, x, y); // the character boundary nearest the point
  if (!block || !hit) return view.setAnnotations([]);
  // Offsets count UTF-16 units of the whole file, front matter included, so the line and
  // column come from the Markdown itself.
  const before = markdown.slice(0, hit.offset).split('\n');
  const [line, column] = [before.length, before.at(-1).length + 1];
  $('where').textContent = t({ en: `Line ${line}, column ${column} · offset ${hit.offset}`,
    es: `Línea ${line}, columna ${column} · posición ${hit.offset}` });
  showSource(block, hit.offset, line);
  // The same ranges, drawn back on the page.
  view.setAnnotations([
    { id: 'block', kind: 'highlight', range: block, color: '#2a9aa6' },
    { id: 'caret', kind: 'insert', range: { start: hit.offset, end: hit.offset } },
  ]);
}
view.on('click', ({ pageIndex, x, y }) => pick(pageIndex, x, y));

المكونات

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

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

#1 · انضد فصلًا بتصميم خاص به

script.js · الأسطر 33–43في الشيفرة الكاملة
const head = (id, content, parity, edge, x, extra = {}) => ({ kind: 'text', id, content, parity,
  pages: 'body', fontFamily: 'IBM Plex Sans', fontSize: pt(7), fontWeight: 500,
  letterSpacing: pt(0.6), color: col('muted'),
  placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(10) } }, ...extra });
const folio = { fontWeight: 600, color: col('accent') };
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', 'top-left', 16, folio),
  head('verso-title', '{title}', 'even', 'top-left', 24),
  head('recto-chapter', '{chapterTitle}', 'odd', 'top-right', -24),
  head('recto-folio', '{pageNumber}', 'odd', 'top-right', -16, folio),
] };

الصفحة صفحة دليل ورشة بلا زخارف: Source Serif 4 بحجم 9.5 نقطة، وعناوين بخط IBM Plex Sans، وترويسات تغيّر جهتها مع الصفحة. لا شيء في التنضيد موضوع من أجل النقرة؛ فكل مستند منضَّد يجيب عن الاستدعاءات نفسها.

#2 · اسأل التنضيد عمّا تحت النقطة

الشيفرة هي الجواب المختصر أعلاه. يعطي الحدث click في العارض الصفحة والنقطة بوحدات بكسل الصفحة، ويقرأ استدعاءان التنضيد عند تلك النقطة. تعدّد sourceAt ما هو مرسوم هناك من الداخل إلى الخارج: الحرف المرسوم، وسطره، وكتلته، وإطارًا حوله إن وُجد. الفقرة الموزعة على صفحتين تجيب بمداها كله، والترويسة تجيب ببند الإعدادات الذي وضعها لا بنص Markdown. وتعطي caretAt أقرب حدٍّ بين حرفين، كما تفعل نقرة في محرر نصوص. كلاهما يعدّ بوحدات UTF-16 في الملف مع مقدمته (front matter)، فيُستخرج السطر والعمود من Markdown نفسه.

#3 · اعرض السطر في الأصل

script.js · الأسطر 149–177في الشيفرة الكاملة
function showSource(block, offset, line) {
  const rows = [];
  let at = 0;
  for (const [i, text] of markdown.split('\n').entries()) {
    const row = document.createElement('div');
    row.className = 'row';
    row.dataset.n = String(i + 1);
    const body = row.appendChild(document.createElement('span')); // one grid cell
    // Cut the line where the block starts and ends and where the caret is.
    const cuts = [at, at + text.length];
    for (const c of [block.start, block.end, offset]) if (c > at && c < cuts[1]) cuts.push(c);
    cuts.sort((a, b) => a - b);
    for (let k = 0; k < cuts.length - 1; k++) {
      const [from, to] = [cuts[k], cuts[k + 1]];
      if (from === offset) body.append(Object.assign(document.createElement('span'),
        { className: 'caret' }));
      const inBlock = from >= block.start && to <= block.end;
      body.append(inBlock ? Object.assign(document.createElement('mark'),
        { textContent: markdown.slice(from, to) }) : markdown.slice(from, to));
    }
    if (offset === cuts.at(-1) && offset > at) body.append(Object.assign(
      document.createElement('span'), { className: 'caret' })); // a caret at the line's end
    rows.push(row);
    at += text.length + 1;
  }
  $('lines').replaceChildren(...rows);
  const target = rows[line - 1];
  $('lines').scrollTop = target.offsetTop - $('lines').clientHeight / 3;
}

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

الوصفة كاملة

// ═══ Postext Cookbook · Nº 216 · Click on the page to find the source ═════════════════
// https://postext.dev/en/cookbook/click-to-markdown-source
// Code: MIT · Text: original (CC BY 4.0) · Pictures: none
// Fonts: Source Serif 4, IBM Plex Sans (SIL OFL 1.1) · Needs postext ≥ 1.103.0
//
// A chapter of a workshop manual in postext-viewer, with its Markdown beside it. A click on a
// page asks the layout which character and which block lie under the point, and the source
// panel scrolls to that line with the block marked and a caret at the character, the way a
// CMS jumps from a proof to the text an author has to change.
import { buildDocumentWithFonts, renderPageToCanvas, sourceAt, caretAt, rangeRects }
  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 = 'click-to-markdown-source';

// ─── 1 · Design ─────────────────────────────────────────────────────────────
// #region palette: semantic colours, every one linked by id
const palette = {
  ink: '#1d2125', // text
  accent: '#1f6f78', // workshop teal: headings' rule, folios
  muted: '#68707a', // 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.5; // pt of body leading

// #region book: a manual's page with running heads by parity
const head = (id, content, parity, edge, x, extra = {}) => ({ kind: 'text', id, content, parity,
  pages: 'body', fontFamily: 'IBM Plex Sans', fontSize: pt(7), fontWeight: 500,
  letterSpacing: pt(0.6), color: col('muted'),
  placement: { anchor: { to: 'page', edge }, offset: { x: mm(x), y: mm(10) } }, ...extra });
const folio = { fontWeight: 600, color: col('accent') };
const header = { elements: [
  head('verso-folio', '{pageNumber}', 'even', 'top-left', 16, folio),
  head('verso-title', '{title}', 'even', 'top-left', 24),
  head('recto-chapter', '{chapterTitle}', 'odd', 'top-right', -24),
  head('recto-folio', '{pageNumber}', 'odd', 'top-right', -16, folio),
] };
// #endregion

const config = {
  locale: t({ en: 'en-gb', es: 'es' }),
  colorPalette,

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

تنويعات

#علّم الكلمة لا الفقرة

توسّع wordAt الموضع إلى الكلمة التي تحت النقطة، بحسب لغة المستند.

-  const block = chain.find((e) => e.level === 'block' && e.source.kind === 'markdown')?.source;
+  const block = wordAt(doc, pageIndex, x, y, markdown); // import wordAt from postext

#علّم السطر المطبوع

العنصر line في السلسلة هو مقطع Markdown الذي يُنضَّد منه سطر واحد من الصفحة، وهو ما يشير إليه المصحِّح.

-  const block = chain.find((e) => e.level === 'block' && e.source.kind === 'markdown')?.source;
+  const block = chain.find((e) => e.level === 'line' && e.source.kind === 'markdown')?.source;

الحقوق

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