### Setup and Installation Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Commands to clone the repository, install dependencies, and execute library examples. ```shell $ git clone https://github.com/jonschlinkert/gray-matter my-project $ cd my-project && npm install $ node examples/ ``` -------------------------------- ### Install yfm using npm Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This command installs the yfm library and saves it as a dependency in your project's package.json file. ```bash npm i yfm --save ``` -------------------------------- ### Install Gray Matter Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Installs the gray-matter package using npm. This is the first step to using the library in your Node.js project. ```bash npm install --save gray-matter ``` -------------------------------- ### Configure Custom Delimiters Source: https://github.com/jonschlinkert/gray-matter/blob/master/benchmark/fixtures/complex.md Example of passing an options object to yfm to define custom open and close delimiters for front-matter parsing. ```javascript { close: ["---", "~~~"], open: ["...", "---"] } ``` -------------------------------- ### TypeScript Usage with Gray-Matter Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Demonstrates how to use gray-matter with TypeScript for type safety and IDE support. Includes examples for basic usage, reading files with options, and stringifying content with type annotations. Requires 'gray-matter' and its TypeScript definitions. ```typescript import matter = require('gray-matter'); // Basic usage with types interface PostData { title: string; date: string; tags?: string[]; } const result = matter>('---\ntitle: Hello\ndate: 2024-01-15\n---\nContent'); const data = result.data as PostData; console.log(data.title); // 'Hello' console.log(result.content); // 'Content' // Read file with options const file = matter.read>('./post.md', { excerpt: true, language: 'yaml' }); // Stringify with type safety const output: string = matter.stringify('Content', { title: 'Test' }); ``` -------------------------------- ### Parse front-matter from a string Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md A basic example of passing a string containing YAML front-matter to the matter function to retrieve a structured object. ```javascript console.log(matter('---\ntitle: Front Matter\n---\nThis is content.')); ``` -------------------------------- ### CoffeeScript Function Definition and Usage in Gray-matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/examples/fixtures/coffee-auto.md Defines a CoffeeScript function 'reverse' to reverse a string and then uses it within Gray-matter's templating syntax along with a 'description' variable. This showcases dynamic content generation. ```coffeescript title: 'coffee functions' user: 'jonschlinkert' reverse: (src) -> src.split('').reverse().join('') --- <%= description %> <%= reverse(user) %> ``` -------------------------------- ### Extract and Access Front-Matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/benchmark/fixtures/complex.md Shows how to extract front-matter from a file and access the returned context object or the full parsed result. ```javascript console.log(yfm(foo.html)); console.log(yfm(foo.html).context); ``` -------------------------------- ### Template syntax usage Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/lang-javascript-fn.md Demonstrates the use of template tags for description and function invocation within the project's templating engine. ```text {%= description %} {%= reverse(user) %} ``` -------------------------------- ### Parse front-matter from a file using Node.js Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Demonstrates how to read a file from the filesystem and parse its front-matter using the gray-matter library. ```javascript const fs = require('fs'); const matter = require('gray-matter'); const str = fs.readFileSync('example.html', 'utf8'); console.log(matter(str)); ``` -------------------------------- ### Define YAML Front Matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md Demonstrates the syntax for adding YAML front matter to documents. This metadata is injected into project templates to provide context-specific data. ```yaml --- username: jonschlinkert --- ``` -------------------------------- ### Parse Raw Strings with Options Source: https://github.com/jonschlinkert/gray-matter/blob/master/benchmark/fixtures/complex.md Demonstrates parsing a raw string by setting the 'read' option to false, preventing the library from attempting to read from the file system. ```javascript yfm("---\nTitle: YFM\n---\nContent.", {read: false}) ``` -------------------------------- ### Extracting Excerpts with Options Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Demonstrates how to extract excerpts using boolean flags or custom functions to control the output content. ```javascript const str = '---\nfoo: bar\n---\nThis is an excerpt.\n---\nThis is content'; const file = matter(str, { excerpt: true }); function firstFourLines(file, options) { file.excerpt = file.content.split('\n').slice(0, 4).join(' '); } const fileCustom = matter(str, { excerpt: firstFourLines }); ``` -------------------------------- ### Basic Usage of yfm Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This JavaScript code demonstrates the basic usage of the yfm library by parsing a file named 'yfm.html'. It requires the 'yfm' module and then calls it with the file path. ```javascript var yfm = require('yfm'); yfm('yfm.html'); ``` -------------------------------- ### Define Configuration Data Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md Provides a sample YAML configuration block used for project metadata and build settings. ```yaml foo: bar version: 2 ``` -------------------------------- ### Configure Custom Parsing Engines Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Demonstrates how to define custom parsing engines for front-matter using the `engines` option. Engines can be provided as functions or objects with `parse` and optional `stringify` methods. ```javascript const toml = require('toml'); // Engine defined as a function const file1 = matter(str, { engines: { toml: toml.parse.bind(toml), } }); // Engine defined as an object const file2 = matter(str, { engines: { toml: { parse: toml.parse.bind(toml), stringify: function() { throw new Error('cannot stringify to TOML'); } } } }); console.log(file1); console.log(file2); ``` -------------------------------- ### Stringify front matter to YAML Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Illustrates how to use the `matter.stringify` method to convert front matter data into a string format, typically YAML, and prepend it to the content. This is useful for writing files with front matter. ```javascript console.log(matter.stringify('foo bar baz', {title: 'Home'})); // results in: // --- // title: Home // --- // foo bar baz ``` -------------------------------- ### options.language Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Defines the engine to use for parsing front-matter. Defaults to 'yaml'. ```APIDOC ## options.language ### Description Define the engine to use for parsing front-matter. If not specified, gray-matter will attempt to detect the language automatically from the delimiter block. ### Parameters #### Request Body - **language** (String) - Optional - The name of the engine to use (e.g., 'toml', 'yaml'). ### Request Example ```js matter(string, {language: 'toml'}); ``` ### Response #### Success Response (200) - **content** (String) - The parsed content body. - **data** (Object) - The parsed front-matter object. #### Response Example ```js { content: 'This is content', data: { title: 'TOML' } } ``` ``` -------------------------------- ### Extracting Front Matter and Content from a File Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This JavaScript code snippet demonstrates how to extract both the parsed front matter (context) and the content from an HTML file named 'foo.html' using the yfm library. ```javascript console.log(yfm('foo.html')); ``` -------------------------------- ### options.delimiters Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Configures the opening and closing delimiters for the front-matter block. ```APIDOC ## options.delimiters ### Description Allows customization of the delimiters used to identify the front-matter block. Can be a single string or an array of two strings. ### Parameters #### Request Body - **delims** (String|Array) - Optional - The delimiter string or array of [open, close] delimiters. Defaults to '---'. ### Request Example ```js // Using an array matter.read('file.md', {delims: ['~~~', '~~~']}); ``` ### Response #### Success Response (200) - **data** (Object) - The parsed front-matter object using the custom delimiters. ``` -------------------------------- ### Read and parse front matter from a file Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Demonstrates the synchronous method `matter.read` for reading a file from the file system and parsing its front matter. It returns the same object structure as the main `matter` function. ```javascript const file = matter.read('./content/blog-post.md'); ``` -------------------------------- ### Use Handlebars Template Comments Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md Shows the syntax for including comments within markdown templates. These comments are stripped during the rendering process. ```handlebars [[!-- foo --]] [[! foo ]] [[!foo]] ``` -------------------------------- ### Initialize Variable Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/autodetect-javascript.md A basic JavaScript variable declaration snippet used for demonstration purposes. ```javascript var foo = 'bar'; ``` -------------------------------- ### Parse Front Matter and Template Variables Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/lang-javascript-object-fn.md Demonstrates the structure of YAML front-matter containing metadata and a JavaScript function. The template section shows how to access these variables and execute functions within the front-matter context. ```javascript --- { title: 'javascript front matter', user: 'jonschlinkert', fn: { reverse: function(str) { return str.split('').reverse().join(''); } } } --- {%= description %} {%= reverse(user) %} ``` -------------------------------- ### Define Front-Matter and Template Logic Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/autodetect-javascript.md Demonstrates the structure of front-matter data including a nested function definition. This pattern allows for dynamic content generation using template tags like {%= description %} and {%= reverse(user) %}. ```javascript --- javascript { title: 'autodetect-javascript', user: 'jonschlinkert', fn: { reverse: function(str) { return str.split('').reverse().join(''); } } } --- {%= description %} {%= reverse(user) %} ``` -------------------------------- ### Parse YAML Front-Matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/benchmark/fixtures/complex.md Basic usage of the yfm library to parse content. It demonstrates how to require the module and process a string or file content. ```javascript var yfm = require("yfm"); yfm(yfm.html); ``` -------------------------------- ### Import gray-matter in Node.js and TypeScript Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Shows the standard methods for importing the gray-matter library into a project using CommonJS or TypeScript syntax. ```javascript const matter = require('gray-matter'); ``` ```typescript import matter = require('gray-matter'); // OR import * as matter from 'gray-matter'; ``` -------------------------------- ### Parsing Raw String Content with yfm Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This JavaScript code shows how to use the yfm library to parse a raw string without reading from the file system. The `read: false` option is used, and the function is called with the string content and options. ```javascript yfm('---\nTitle: YFM\n---\nContent.', {read: false}) ``` -------------------------------- ### Configuring Excerpt Separators Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Shows how to define a custom string separator to identify the end of an excerpt within the content. ```javascript console.log(matter(string, { excerpt_separator: '' })); ``` -------------------------------- ### Configure Front-Matter Language with Options Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Specify the engine to use for parsing front-matter by setting the 'language' option. This allows you to control how front-matter is interpreted, supporting various formats like YAML and TOML. The default language is 'yaml'. ```javascript console.log(matter(string, {language: 'toml'})); ``` -------------------------------- ### Specify Front-Matter Language Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Shows how to specify the language for parsing front-matter using the `language` option. This is useful when the front-matter is not in the default YAML format. ```javascript const string = '---\ntitle = "TOML"\ndescription = "Front matter"\ncategories = "front matter toml"\n---' + '\nThis is content'; console.log(matter(string, {language: 'toml'})); ``` -------------------------------- ### Custom Delimiters for YAML Front-Matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This JavaScript object defines custom open and close delimiters for YAML front-matter. It allows specifying multiple patterns as strings or arrays of strings for flexible parsing. ```javascript { close: ['---', '~~~'], open: ['...', '---'] } ``` -------------------------------- ### Accessing Parsed YAML Front Matter Context Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This JavaScript code shows how to access only the parsed YAML front matter (context) from the result of the yfm function when applied to 'foo.html'. ```javascript console.log(yfm('foo.html').context); ``` -------------------------------- ### Extract Excerpts with Gray-Matter (JavaScript) Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Demonstrates how to extract excerpts from content using gray-matter. Supports default delimiters, custom separators, and custom extraction functions. Requires the 'gray-matter' package. ```javascript const matter = require('gray-matter'); // Extract excerpt using default delimiter (---) const file1 = matter([ '---', 'title: Blog Post', '---', 'This is the excerpt.', '---', 'This is the full content.' ].join('\n'), { excerpt: true }); console.log(file1.excerpt); // 'This is the excerpt.\n' // Extract excerpt with custom separator const file2 = matter([ '---', 'title: Blog Post', '---', 'This is an excerpt.', '', 'This is content' ].join('\n'), { excerpt_separator: '' }); console.log(file2.excerpt); // 'This is an excerpt.' // Extract excerpt with custom function function firstFourLines(file, options) { file.excerpt = file.content.split('\n').slice(0, 4).join(' '); } const file3 = matter([ '---', 'foo: bar', '---', 'Line one', 'Line two', 'Line three', 'Line four', 'Line five' ].join('\n'), { excerpt: firstFourLines }); console.log(file3.excerpt); // 'Line one Line two Line three Line four' ``` -------------------------------- ### Extract Excerpt with Custom Function Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Shows how to use a custom function for the `excerpt` option to control excerpt extraction. The function receives `file` and `options` as parameters. ```javascript // returns the first 4 lines of the contents function firstFourLines(file, options) { file.excerpt = file.content.split('\n').slice(0, 4).join(' '); } const file = matter([ '---', 'foo: bar', '---', 'Only this', 'will be', 'in the', 'excerpt', 'but not this...' ].join('\n'), {excerpt: firstFourLines}); console.log(file); ``` -------------------------------- ### Configure Front-Matter Delimiters Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Customize the open and close delimiters used for front-matter. Delimiters can be provided as a single string for both open and close, or as an array of two strings specifying the open and close delimiters separately. The default delimiter is '---'. ```javascript // format delims as a string matter.read('file.md', {delims: '~~~'}); // or an array (open/close) matter.read('file.md', {delims: ['~~~', '~~~']}); ``` ```html --- title: Home --- This is the {{title}} page. ``` -------------------------------- ### Extract Excerpt with Boolean Option Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Demonstrates how to extract an excerpt from a string using the `excerpt: true` option. The excerpt is the content directly following the front-matter delimiters. ```javascript const str = '---\nfoo: bar\n---\nThis is an excerpt.\n---\nThis is content'; const file = matter(str, { excerpt: true }); console.log(file); ``` -------------------------------- ### Manage Caching in Gray Matter Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Demonstrates how Gray Matter automatically caches parsed content and how to manually clear the cache. It also highlights that passing configuration options will bypass the internal cache. ```javascript const matter = require('gray-matter'); // Parsing is cached automatically (when no options are passed) const content = '---\ntitle: Cached\n---\nContent'; const file1 = matter(content); // Parsed and cached const file2 = matter(content); // Retrieved from cache // Clear cache when needed matter.clearCache(); // Cache is bypassed when options are passed const file3 = matter(content, { excerpt: true }); // Not cached ``` -------------------------------- ### Checking for YAML Front Matter Existence Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/complex.md This JavaScript function, `hasYFM`, checks if a given source string contains any YAML front matter by parsing it with yfm and examining the length of the returned context object's keys. ```javascript var hasYFM = function (src, options) { var obj = yfm(src, options).context; return _.keys(obj).length > 0; }; ``` -------------------------------- ### Register Custom Parsing Engines with Gray-Matter (JavaScript) Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Shows how to register custom parsing engines for formats like TOML and CoffeeScript with gray-matter. Requires 'gray-matter', 'toml', and 'coffeescript' packages. ```javascript const matter = require('gray-matter'); const toml = require('toml'); const coffee = require('coffeescript'); // Add TOML engine const tomlContent = [ '---toml', 'title = "TOML Front Matter"', 'description = "Example document"', '[author]', 'name = "John"', '---', 'This is content' ].join('\n'); const tomlFile = matter(tomlContent, { engines: { toml: toml.parse.bind(toml) } }); console.log(tomlFile.data); // { title: 'TOML Front Matter', description: 'Example document', author: { name: 'John' } } // Add CoffeeScript engine with parse and stringify const coffeeContent = [ '---coffee', 'title: "CoffeeScript"', 'tags: ["front-matter", "coffee"]', '---', 'Content here' ].join('\n'); const coffeeFile = matter(coffeeContent, { engines: { coffee: { parse: function(str, options) { return coffee['eval'](str, options); }, stringify: function(obj) { // Custom stringify logic return JSON.stringify(obj, null, 2); } } } }); console.log(coffeeFile.data); // { title: 'CoffeeScript', tags: ['front-matter', 'coffee'] } // Set default language for files without language identifier const yamlFile = matter.read('./config.md', { language: 'toml', engines: { toml: toml.parse.bind(toml) } }); ``` -------------------------------- ### Define Custom Excerpt Separator Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Illustrates how to define a custom separator for excerpts using the `excerpt_separator` option. This allows for more flexible excerpt definition within the content. ```javascript const string = '---\ntitle: Blog\n---\nMy awesome blog.\n\n

Hello world

'; console.log(matter(string, {excerpt_separator: ''})); ``` -------------------------------- ### Parse TOML Front-Matter from HTML String Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Demonstrates parsing an HTML string with TOML front-matter. The 'language' option is set to 'toml', enabling gray-matter to correctly parse the TOML data into the 'data' property of the returned object. The remaining content is stored in the 'content' property. ```html --- title = "TOML" description = "Front matter" categories = "front matter toml" --- This is content ``` ```javascript { content: 'This is content', excerpt: '', data: { title: 'TOML', description: 'Front matter', categories: 'front matter toml' } } ``` -------------------------------- ### Defining Custom Engines Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Configures gray-matter to use external libraries for parsing non-default formats like TOML by providing custom parse and stringify methods. ```javascript const toml = require('toml'); const file = matter(str, { engines: { toml: { parse: toml.parse.bind(toml), stringify: function() { throw new Error('cannot stringify to TOML'); } } } }); ``` -------------------------------- ### Dynamic Language Detection Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Illustrates gray-matter's ability to dynamically detect the front-matter language from the content itself, by specifying the language after the opening delimiter. ```html ---\ntoml title = "TOML" description = "Front matter" categories = "front matter toml" --- This is content ``` -------------------------------- ### Define CSON Front Matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/lang-cson.md Demonstrates the syntax for CSON front matter within a document. This format allows for CoffeeScript-style object definitions at the top of a file. ```cson --- title: 'CSON' description: ''' Front matter ''' categories: ''' front matter cson ''' --- ``` -------------------------------- ### Define and Use CoffeeScript Reverse Function Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/lang-coffee-fn.md This snippet defines a simple CoffeeScript function `reverse` that takes a string, splits it into an array of characters, reverses the array, and then joins the characters back into a string. It also shows how to call this function with a variable. ```coffeescript reverse = (src) -> src.split('').reverse().join('') ``` ```html {%= description %} {%= reverse(user) %} ``` -------------------------------- ### CoffeeScript String Reversal Function Source: https://github.com/jonschlinkert/gray-matter/blob/master/examples/fixtures/coffee.md Demonstrates a simple CoffeeScript function to reverse a given string. It splits the string into an array of characters, reverses the array, and then joins the characters back into a string. This function is self-contained and does not require external dependencies. ```coffeescript reverse = (src) -> src.split('').reverse().join('') ``` -------------------------------- ### Read and Parse File with Front-Matter Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Synchronously reads a file from the file system and parses its front-matter. Returns the same object structure as the main `matter` function with an additional `path` property. Supports custom language and parsing engines. ```javascript const matter = require('gray-matter'); // Read and parse a markdown file const file = matter.read('./content/blog-post.md'); console.log(file.data); // { title: 'My Post', date: '2024-01-15' } console.log(file.content); // 'The blog post content...' console.log(file.path); // './content/blog-post.md' // Read with custom language option const tomlFile = matter.read('./config.md', { language: 'toml', engines: { toml: require('toml').parse.bind(require('toml')) } }); ``` -------------------------------- ### Stringify Data back to Front-Matter Format Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Converts an object back into a string with front-matter. It takes content and data, wraps the data in delimiters, and prepends it to the content. Supports specifying the output language format. ```javascript const matter = require('gray-matter'); // Basic stringify const output = matter.stringify('This is content.', { title: 'Home', draft: false }); console.log(output); // --- // title: Home // draft: false // --- // This is content. // Stringify with parsed file object const file = matter('---\nfoo: bar\n---\nOriginal content'); const modified = file.stringify({ baz: ['one', 'two', 'three'] }); console.log(modified); // --- // baz: // - one // - two // - three // --- // Original content // Stringify to JSON format const jsonOutput = matter.stringify('Content', { name: 'test' }, { language: 'json' }); console.log(jsonOutput); // ---json // { // "name": "test" // } // --- // Content ``` -------------------------------- ### Parse Documents with Multiple Sections using Gray-Matter (JavaScript) Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Illustrates parsing documents with multiple front-matter sections using gray-matter and js-yaml. Handles complex structured content by defining a custom section parsing function. Requires 'gray-matter' and 'js-yaml' packages. ```javascript const matter = require('gray-matter'); const yaml = require('js-yaml'); // Document with multiple sections const content = [ '---yaml', 'title: Main Document', '---', 'Introduction content.', '', '---section1', 'heading: First Section', '---', 'Section one content.', '', '---section2', 'heading: Second Section', '---', 'Section two content.' ].join('\n'); const file = matter(content, { section: function(section, file) { // Parse YAML data in each section if (typeof section.data === 'string' && section.data.trim() !== '') { section.data = yaml.safeLoad(section.data); } section.content = section.content.trim() + '\n'; } }); console.log(file.data); // { title: 'Main Document' } console.log(file.sections); // [ // { key: 'section1', data: { heading: 'First Section' }, content: 'Section one content.\n' }, // { key: 'section2', data: { heading: 'Second Section' }, content: 'Section two content.\n' } // ] ``` -------------------------------- ### Test for the presence of front matter Source: https://github.com/jonschlinkert/gray-matter/blob/master/README.md Explains the `matter.test` function, which returns a boolean indicating whether the given string contains front matter. This can be used for pre-validation before attempting to parse. ```javascript const hasMatter = matter.test('---\ntitle: Test\n---\nContent'); //=> true ``` -------------------------------- ### Parse Front-Matter from String Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Parses front-matter from a given string. It returns an object containing the parsed data, the content, and other metadata. Supports automatic language detection and custom delimiters. ```javascript const matter = require('gray-matter'); // Basic YAML front-matter parsing const result = matter('---\ntitle: Hello World\nauthor: John Doe\n---\nThis is the content.'); console.log(result); // { // data: { title: 'Hello World', author: 'John Doe' }, // content: 'This is the content.', // excerpt: '', // orig: '---\ntitle: Hello World\nauthor: John Doe\n---\nThis is the content.', // language: 'yaml', // matter: 'title: Hello World\nauthor: John Doe' // } // Parse JSON front-matter (auto-detected) const jsonFile = matter([ '---json', '{', ' "name": "gray-matter",', ' "version": "4.0.0"', '}', '---', 'This is content' ].join('\n')); console.log(jsonFile.data); // { name: 'gray-matter', version: '4.0.0' } // Parse with custom delimiters const customDelims = matter('~~~ title: Custom\n~~~\nContent here', { delimiters: '~~~' }); console.log(customDelims.data); // { title: 'Custom' } ``` -------------------------------- ### Detect Front-Matter Language Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Detects the front-matter language specified after the opening delimiter. Returns an object with the raw detected string and the trimmed language name. Handles cases with no explicit language. ```javascript const matter = require('gray-matter'); // Detect language from string console.log(matter.language('---yaml\ntitle: Hello\n---')); // { raw: 'yaml', name: 'yaml' } console.log(matter.language('---json\n{"key": "value"}\n---')); // { raw: 'json', name: 'json' } console.log(matter.language('---\ntitle: Hello\n---')); // { raw: '', name: '' } ``` -------------------------------- ### Auto-Detect Front-Matter Language Source: https://github.com/jonschlinkert/gray-matter/blob/master/.verb.md Gray-matter can automatically detect the front-matter language by examining the content immediately following the opening delimiter. This eliminates the need to explicitly set the 'language' option, making parsing more flexible. ```html --- toml title = "TOML" description = "Front matter" categories = "front matter toml" --- This is content ``` -------------------------------- ### Test for Front-Matter in String Source: https://context7.com/jonschlinkert/gray-matter/llms.txt Returns true if the given string contains front-matter, and false otherwise. This is useful for checking content before attempting to parse it. Supports custom delimiters. ```javascript const matter = require('gray-matter'); // Check if string has front-matter console.log(matter.test('---\ntitle: Hello\n---\nContent')); // true console.log(matter.test('No front matter here')); // false // Test with custom delimiters console.log(matter.test('~~~ title: Hello\n~~~', { delimiters: '~~~' })); // true ``` -------------------------------- ### CSON String Reversal Function Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/autodetect-cson-fn.md Defines and demonstrates a CSON function to reverse a given string. This function takes a string as input and returns its reversed version. It utilizes basic string manipulation methods available in JavaScript. ```cson title: 'CSON functions' user: 'jonschlinkert' fn: reverse = (src) -> src.split('').reverse().join('') {%= description %} {%= reverse(user) %} ``` -------------------------------- ### Reverse string utility function Source: https://github.com/jonschlinkert/gray-matter/blob/master/test/fixtures/lang-javascript-fn.md A JavaScript function that reverses a provided string by splitting it into an array of characters, reversing the array, and joining it back together. It takes a string as input and returns the reversed string as output. ```javascript function reverse(str) { return str.split('').reverse().join(''); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.