wayver's git archive


an obsidian renderer
git clone https://git.wayver.dev/sable

sable-markdown/src/parser/inline/environment_variable.rs@337ba67f65eaa17b44e371af7c0f0c761d6aa914

raw
Date Commit Message Author Files + -
2026-02-23 21:59 beginning on port over some of the changes from markdown-ppp since the f... wayverd 11 527 15
...

1use nom::{
2    IResult, Parser,
3    branch::alt,
4    character::complete::{alpha1, alphanumeric1, char},
5    combinator::{map, recognize, verify},
6    multi::many0,
7    sequence::pair,
8};
9
10use crate::ast::Inline;
11
12pub(super) fn environment_variable(input: &str) -> IResult<&str, Inline> {
13    map(
14        verify(
15            recognize(pair(
16                alpha1,
17                many0(alt((alphanumeric1, recognize(char('_'))))),
18            )),
19            |s: &str| is_likely_env_var(s),
20        ),
21        |var_name: &str| Inline::Text(var_name.to_string()),
22    )
23    .parse(input)
24}
25
26fn is_likely_env_var(s: &str) -> bool {
27    // Must contain at least one underscore
28    if !s.contains('_') {
29        return false;
30    }
31
32    // Must not start or end with underscore
33    if s.starts_with('_') || s.ends_with('_') {
34        return false;
35    }
36
37    // Must not have consecutive underscores
38    if s.contains("__") {
39        return false;
40    }
41
42    // Should be reasonable length (heuristic)
43    if s.len() < 3 || s.len() > 50 {
44        return false;
45    }
46
47    true
48}
49