DEV Community

Cover image for Markdown Tilde Fence Validation
Raylabs
Raylabs

Posted on Originally published at raylabs.app

Markdown Tilde Fence Validation

Technical article quality gates frequently reject valid drafts because strict parsers fail to recognize alternative syntax. A technical draft contained seven tilde-fenced configuration or code examples, yet the quality check reported that it lacked a concrete code example because its detector looked exclusively for backticks.

Quick solution:

function hasValidCodeBlock(content: string): boolean {
  const fenceRegex = /^(?:~{3,}|`{3,})/m;
  return fenceRegex.test(content);
}
Enter fullscreen mode Exit fullscreen mode

This validator implementation reuses existing rules to detect both tilde and backtick fences without adding external dependencies or new parsers.

The Markdown Dialect Disconnect

The CommonMark specification permits both backticks and tildes for enclosing code blocks. Many publishing platforms use CommonMark Spec rules for rendering while relying on custom regular expressions for internal validation checks. When a quality pipeline scans a draft for required code examples, a naive regex targeting only backticks will produce false negatives for valid tilde fences.

When building a remote publication pipeline, similar parser inconsistencies can surface across deployment environments, much like configuring an environment for Building A Remote Only Android Repository. Ensuring uniform parsing logic prevents arbitrary build failures.

Implementation Strategy

To resolve the validation gap without introducing heavy dependencies or altering storage formats, the fix must reuse the established markdown reader. Instead of writing a dedicated second parser, hook into the existing tokenization pass.

Option When to Use Trade-off or Failure Mode Recommendation
Custom Regex Check Simple keyword scanning Misses alternative valid syntaxes like tilde fences Avoid for multi-dialect documents
Shared Fence Reader Reusing production rendering parsers Slightly higher initial setup complexity Recommended for parity
Strict Backtick Policy Enforcing rigid house style Frustrates authors using nested code blocks Not recommended

Verification Checklist

  • Confirm that the parser routine recognizes both triple backticks and triple tildes as valid code block delimiters.
  • Run existing test suites, including type checking and coverage thresholds, to verify no regressions in other validation rules.
  • Execute a local deployment dry-run, such as a Wrangler test run referencing Cloudflare Workers Limits, to confirm local-remote behavioral parity.
  • Verify that a draft containing only tilde-fenced examples successfully passes the code-presence requirement.

Top comments (0)