Strip out YAML blocks

Is there an easy way to strip yaml blocks from markdown files, or more concretely - anything between and including the first and second occurence of --- and ---.

wow, can’t believe I just wrote something (jank 1000) for this last week:

if you want the block inclusively, just remove +1 and -1

now that I’m posting my inefficient code, I have to make a version where you don’t have to read the whole file:

function revise(path)    
    t = open(path)       
    lines = []               
    c = 0                
    while c<2       
        r = readline(t)
        (r == "---" && (c+=1))              
        push!(lines, r)                  
    end                           
    return join(lines, "\n")    
end  
1 Like

Thanks. I reworked your example to keep the md and remove the YAML:

# Remove YAML block
mdlines = readlines(file)
md = join(mdlines[findall(x -> x=="---", mdlines)[2]+1:end], "\n")
write(file, md)

ah, didn’t realize stripe means get rid off, I thought you want to extract that block of YAML (biased based on my use case of course), in that case readlines should be just fine.

No worries! It did most of what I needed. I’m trying to use .jmd files from Weave.jl in Documenter.jl, and Documenter doesn’t seem to handle .jmd yet so I’m just deleting the YAML