@simple-mark/model 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -7,7 +7,8 @@ or a canvas renderer.
7
7
  - Parses CommonMark + GFM + `$math$` (Lezer) into **styled ranges**: line styles (headings,
8
8
  quotes, code blocks, tables, …) and non-overlapping inline spans (strong, em, code, link,
9
9
  and `mark` for the syntax characters, which stay visible and dimmed).
10
- - A **theme** as plain data, and `resolveLook` to turn styles into font/color properties.
10
+ - A **theme** as plain data (the default follows GitHub's Markdown styles), and `resolveLook` /
11
+ `resolveLineLook` to turn styles into font, color and line properties.
11
12
  - **Tables**: display width (Korean, Chinese, Japanese count as two columns), row splitting,
12
13
  and re-aligning a table by padding its cells.
13
14
 
package/index.d.ts CHANGED
@@ -159,6 +159,8 @@ interface Look {
159
159
  color?: string;
160
160
  /** Behind the styled text only (e.g. inline code). */
161
161
  background?: string;
162
+ /** Corner radius of `background`, in em of the line's font. */
163
+ radius?: number;
162
164
  strike?: boolean;
163
165
  underline?: boolean;
164
166
  }
@@ -166,9 +168,33 @@ interface Look {
166
168
  interface LineLook extends Look {
167
169
  /** Behind the full width of the line (e.g. code blocks). */
168
170
  lineBackground?: string;
169
- /** A bar along the left edge of the line (e.g. quotes). */
171
+ /** Corner radius of the block's `lineBackground` (top of its first line, bottom of its last), in em. */
172
+ lineRadius?: number;
173
+ /** A bar along the left edge of the line (e.g. quotes), `STROKES.bar` wide. */
170
174
  borderLeft?: string;
175
+ /** Space before the text, in em of the base font. Nested blocks add up (code in a quote). */
176
+ indent?: number;
177
+ /** A line under the block's last line (h1, h2), `STROKES.border` thick. */
178
+ borderBottom?: string;
179
+ /** A line across the middle of the line (horizontal rules), `STROKES.rule` thick. */
180
+ rule?: string;
171
181
  }
182
+ /** A line's look with its blocks' edges applied: `lineRadius` becomes the corners this line rounds. */
183
+ interface ResolvedLineLook extends Omit<LineLook, 'lineRadius'> {
184
+ radiusTop?: number;
185
+ radiusBottom?: number;
186
+ }
187
+ /** Which of a line's blocks start and end on it. */
188
+ interface BlockEdges {
189
+ first: Iterable<BlockStyle>;
190
+ last: Iterable<BlockStyle>;
191
+ }
192
+ /** Widths of bars and lines, in em of the base font (1px and 4px at 16px, as GitHub). */
193
+ export declare const STROKES: {
194
+ readonly bar: 0.25;
195
+ readonly border: number;
196
+ readonly rule: 0.25;
197
+ };
172
198
  interface Theme {
173
199
  fontFamily: string;
174
200
  monospaceFamily: string;
@@ -177,9 +203,12 @@ interface Theme {
177
203
  inline: Record<InlineStyle, Look>;
178
204
  }
179
205
  export declare const defaultTheme: Theme;
180
- /** The look of a line with these block styles (any order). */
181
- export declare function resolveLineLook(blocks: Iterable<BlockStyle>, theme?: Theme): LineLook;
206
+ /**
207
+ * The look of a line with these block styles (any order). `edges` tells which blocks start and
208
+ * end on the line (for corners and bottom borders); without it every block is one line long.
209
+ */
210
+ export declare function resolveLineLook(blocks: Iterable<BlockStyle>, theme?: Theme, edges?: BlockEdges): ResolvedLineLook;
182
211
  /** The look of text with these block and inline styles (any order). */
183
212
  export declare function resolveLook(blocks: Iterable<BlockStyle>, inline: Iterable<InlineStyle>, theme?: Theme): Look;
184
213
  //#endregion
185
- export type { BlockRange, BlockStyle, ColumnAlign, ImageSource, InlineStyle, LineLook, LinkAt, Look, MarkdownStyles, PreviewBlock, RenderedTable, RowCell, Span, Table, Theme };
214
+ export type { BlockEdges, BlockRange, BlockStyle, ColumnAlign, ImageSource, InlineStyle, LineLook, LinkAt, Look, MarkdownStyles, PreviewBlock, RenderedTable, ResolvedLineLook, RowCell, Span, Table, Theme };
package/index.js CHANGED
@@ -630,57 +630,74 @@ function renderTable(table) {
630
630
  }
631
631
  //#endregion
632
632
  //#region packages/model/src/theme.ts
633
+ /** Widths of bars and lines, in em of the base font (1px and 4px at 16px, as GitHub). */
634
+ const STROKES = {
635
+ bar: .25,
636
+ border: 1 / 16,
637
+ rule: .25
638
+ };
639
+ const muted = "#59636e";
640
+ const border = "#d1d9e0";
641
+ const subtleBorder = "#d1d9e0b3";
642
+ const codeBackground = "#f6f8fa";
633
643
  const defaultTheme = {
634
644
  fontFamily: "system-ui, -apple-system, \"Segoe UI\", \"Noto Sans KR\", sans-serif",
635
645
  monospaceFamily: "\"D2Coding\", \"Noto Sans Mono CJK KR\", ui-monospace, \"SFMono-Regular\", Menlo, Consolas, monospace",
636
646
  color: "#1f2328",
637
647
  block: {
638
648
  quote: {
639
- color: "#59636e",
640
- borderLeft: "#d1d9e0"
649
+ color: muted,
650
+ borderLeft: border,
651
+ indent: 1
641
652
  },
642
653
  codeblock: {
643
654
  monospace: true,
644
- lineBackground: "#f6f8fa"
655
+ lineBackground: codeBackground,
656
+ lineRadius: 6 / 16,
657
+ indent: 1
645
658
  },
646
659
  mathblock: {
647
660
  monospace: true,
648
661
  color: "#6639ba"
649
662
  },
650
663
  table: { monospace: true },
651
- tableHeader: { fontWeight: 700 },
652
- tableRowAlt: { lineBackground: "#f6f8fa" },
653
- hr: {},
664
+ tableHeader: { fontWeight: 600 },
665
+ tableRowAlt: { lineBackground: codeBackground },
666
+ hr: { rule: border },
654
667
  h1: {
655
- fontScale: 1.6,
656
- fontWeight: 700
668
+ fontScale: 2,
669
+ fontWeight: 600,
670
+ borderBottom: subtleBorder
657
671
  },
658
672
  h2: {
659
- fontScale: 1.4,
660
- fontWeight: 700
673
+ fontScale: 1.5,
674
+ fontWeight: 600,
675
+ borderBottom: subtleBorder
661
676
  },
662
677
  h3: {
663
- fontScale: 1.2,
664
- fontWeight: 700
678
+ fontScale: 1.25,
679
+ fontWeight: 600
665
680
  },
666
- h4: {
667
- fontScale: 1.1,
668
- fontWeight: 700
681
+ h4: { fontWeight: 600 },
682
+ h5: {
683
+ fontScale: .875,
684
+ fontWeight: 600
669
685
  },
670
- h5: { fontWeight: 700 },
671
686
  h6: {
672
- fontWeight: 700,
673
- color: "#59636e"
687
+ fontScale: .85,
688
+ fontWeight: 600,
689
+ color: muted
674
690
  }
675
691
  },
676
692
  inline: {
677
693
  link: { color: "#0969da" },
678
- strong: { fontWeight: 700 },
694
+ strong: { fontWeight: 600 },
679
695
  em: { italic: true },
680
696
  strike: { strike: true },
681
697
  code: {
682
698
  monospace: true,
683
- background: "rgba(129, 139, 152, 0.15)"
699
+ background: "#818b981f",
700
+ radius: 6 / 16
684
701
  },
685
702
  math: {
686
703
  monospace: true,
@@ -699,19 +716,38 @@ const defaultTheme = {
699
716
  }
700
717
  }
701
718
  };
702
- /** The look of a line with these block styles (any order). */
703
- function resolveLineLook(blocks, theme = defaultTheme) {
719
+ /**
720
+ * The look of a line with these block styles (any order). `edges` tells which blocks start and
721
+ * end on the line (for corners and bottom borders); without it every block is one line long.
722
+ */
723
+ function resolveLineLook(blocks, theme = defaultTheme, edges) {
704
724
  const present = new Set(blocks);
725
+ const first = edges ? new Set(edges.first) : present;
726
+ const last = edges ? new Set(edges.last) : present;
705
727
  const look = {};
706
- for (const style of BLOCK_STYLES) if (present.has(style)) Object.assign(look, theme.block[style]);
728
+ let indent = 0;
729
+ for (const style of BLOCK_STYLES) {
730
+ if (!present.has(style)) continue;
731
+ const { lineRadius, borderBottom, indent: own, ...rest } = theme.block[style];
732
+ Object.assign(look, rest);
733
+ indent += own ?? 0;
734
+ if (borderBottom !== void 0 && last.has(style)) look.borderBottom = borderBottom;
735
+ if (rest.lineBackground !== void 0) {
736
+ delete look.radiusTop;
737
+ delete look.radiusBottom;
738
+ if (lineRadius && first.has(style)) look.radiusTop = lineRadius;
739
+ if (lineRadius && last.has(style)) look.radiusBottom = lineRadius;
740
+ }
741
+ }
742
+ if (indent) look.indent = indent;
707
743
  return look;
708
744
  }
709
745
  /** The look of text with these block and inline styles (any order). */
710
746
  function resolveLook(blocks, inline, theme = defaultTheme) {
711
- const { lineBackground: _bg, borderLeft: _border, ...look } = resolveLineLook(blocks, theme);
747
+ const { lineBackground: _bg, borderLeft: _bar, indent: _indent, borderBottom: _border, rule: _rule, radiusTop: _top, radiusBottom: _bottom, ...look } = resolveLineLook(blocks, theme);
712
748
  const present = new Set(inline);
713
749
  for (const style of INLINE_STYLES) if (present.has(style)) Object.assign(look, theme.inline[style]);
714
750
  return look;
715
751
  }
716
752
  //#endregion
717
- export { BLOCK_STYLES, INLINE_STYLES, cellAt, charWidth, defaultTheme, displayWidth, flattenSpans, headingAnchors, headingSlug, linkAt, linkKind, markdownExtensions, markdownParser, markdownStyles, mathSource, parseTable, previewBlocks, renderTable, resolveLineLook, resolveLook, rowLine, splitRow };
753
+ export { BLOCK_STYLES, INLINE_STYLES, STROKES, cellAt, charWidth, defaultTheme, displayWidth, flattenSpans, headingAnchors, headingSlug, linkAt, linkKind, markdownExtensions, markdownParser, markdownStyles, mathSource, parseTable, previewBlocks, renderTable, resolveLineLook, resolveLook, rowLine, splitRow };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@simple-mark/model",
3
- "version": "0.3.4",
3
+ "version": "0.3.5",
4
4
  "description": "Markdown parsing and styling model shared by every renderer. Pure TypeScript, no DOM.",
5
5
  "license": "MIT",
6
6
  "type": "module",