# Stripping frontmatter

**URL:** https://community.silverbullet.md/t/stripping-frontmatter/3419
**Category:** Troubleshooting
**Created:** [October 19, 2025, 3:46pm UTC](https://community.silverbullet.md/t/stripping-frontmatter/3419 "2025-10-19T15:46:24Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![thepaperpilot](https://community.silverbullet.md/user_avatar/community.silverbullet.md/thepaperpilot/32/1557_2.png) [@thepaperpilot](https://community.silverbullet.md/u/thepaperpilot)
#### Post date: [October 19, 2025, 3:46pm UTC](https://community.silverbullet.md/t/stripping-frontmatter/3419/1 "2025-10-19T15:46:24Z")

</div>

I’m trying to read a page and get just the content of it in space lua, but I’ve struggled with matching newlines.

I’ve gotten this function which will correctly strip frontmatter with only a single line:

```lua
local function strip_frontmatter(text)
  return text:gsub("^%s*%-%-%-[\r\n]+.-[\r\n]+%-%-%-%s*", "")
end

```

but if I have multiple lines within the frontmatter it won’t work. I’ve tried combinations of %s, \r, \n, %w and . but I can’t find a pattern that’ll match newlines and non-newlines (e.g. `[\r\n.]` doesn’t work).

An example frontmatter that I have been unable to strip:

```yaml
---
aliases:
  - Foo
  - Bar
public: true
---

```

tbc, things that I think _ought_ to work based on online resources don’t work in SB, so I’d appreciate any help but please test it within SB specifically first.

---

<div class="post-metadata">

### Author: ![Mr.Red](https://community.silverbullet.md/user_avatar/community.silverbullet.md/mr.red/32/821_2.png) [@Mr.Red](https://community.silverbullet.md/u/Mr.Red)
#### Post date: [October 19, 2025, 4:29pm UTC](https://community.silverbullet.md/t/stripping-frontmatter/3419/2 "2025-10-19T16:29:41Z")

</div>

there already is a functions to strip a text from its frontmatter [API/index Documentation](https://silverbullet.md/API/index)

`index.extractFrontmatter(text, extractOptions)`

## To strip a frontmatter from a page:

```auto
function strip_frontmatter_page(page)
  local fm = index.extractFrontmatter(space.readPage(page), {
    removeFrontMatterSection = true,
    removeTags = false
  })
  return fm.text
end

```

## To strip frontmatter from a text

```auto
function strip_frontmatter_from_text(text)
  local fm = index.extractFrontmatter(text, {
    removeFrontMatterSection = true,
    removeTags = false
  })
  return "Text without frontmatter: " .. fm.text .."\n\nAnd this is the frontmatter:" .. fm.frontmatter
end

```

here is a screenshot with the result for `fm`

 ![image](https://community.silverbullet.md/uploads/default/original/2X/b/b9f48b6967bace2ca40e226a98e007c1f3f86b41.png)
