Skip to content

Commit

Permalink
Add liquid link formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AnimatedSwine37 committed Apr 14, 2024
1 parent 341927a commit 1d29ba2
Show file tree
Hide file tree
Showing 8 changed files with 7,100 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ _site/
.bundle/
vendor/

.vs/
.vs/
node_modules/
17 changes: 8 additions & 9 deletions admin/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local_backend: true

backend:
name: github
repo: AnimatedSwine37/persona-modding-docs
Expand All @@ -14,8 +16,9 @@ collections:
label: Getting Started
label_singular: 'Page'
folder: getting-started
media_folder: assets/images/getting-started/{{dirname}}
media_folder: /assets/images/getting-started/{{dirname}}
create: true
format: jekyll
# adding a nested object will show the collection folder structure
nested:
depth: 5 # max depth to show in the collection tree
Expand All @@ -38,19 +41,18 @@ collections:
name: include_toc
widget: boolean
default: false
required: false
- label: Body
name: body
widget: markdown
# adding a meta object with a path property allows editing the path of entries
# moving an existing entry will move the entire sub tree of the entry to the new location
meta: { path: { widget: string, label: 'Path', index_file: 'index' } }

- name: contributing
label: Contributing
label_singular: 'Page'
folder: contributing
media_folder: assets/images/contributing/{{dirname}}
media_folder: /assets/images/contributing/{{dirname}}
create: true
format: jekyll
# adding a nested object will show the collection folder structure
nested:
depth: 5 # max depth to show in the collection tree
Expand All @@ -69,7 +71,4 @@ collections:
default: false
- label: Body
name: body
widget: markdown
# adding a meta object with a path property allows editing the path of entries
# moving an existing entry will move the entire sub tree of the entry to the new location
meta: { path: { widget: string, label: 'Path', index_file: 'index' } }
widget: markdown
6 changes: 5 additions & 1 deletion admin/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<body>
<!-- Include the script that builds the page and powers Decap CMS -->
<script src="https://unpkg.com/decap-cms@^3.0.0/dist/decap-cms.js"></script>
</body>

<!-- Custom formatters and things -->
<script src="../assets/js/gray-matter.js"></script>
<script src="../assets/js/formatter.js"></script>

</body>
</html>
44 changes: 44 additions & 0 deletions assets/js/formatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function fromFile(content) {
console.log("Running fromFile on")
console.log(content)
// Note that I removed the options object. If stuff breaks maybe add it back
const result = matter.matter(content, {});
// in the absent of a body when serializing an entry we use an empty one
// when calling `toFile`, so we don't want to add it when parsing.
return {
...result.data,
...(result.content.trim() && { body: liquidLinksToNormal(result.content) }),
};
}

function toFile(data) {
console.log("Running toFile on")
console.log(data)

const { body = '', ...meta } = data;

// gray-matter always adds a line break at the end which trips our
// change detection logic
// https://github.com/jonschlinkert/gray-matter/issues/96
// Note that I removed the options object from the stringify call. If stuff breaks maybe add it back
const trimLastLineBreak = body.slice(-1) !== '\n';
const file = matter.stringify(normalLinksToLiquid(body), meta, {});
return trimLastLineBreak && file.slice(-1) === '\n' ? file.slice(0, -1) : file;
}

// Converts liquid link tags to normal links so decap can handle them
function liquidLinksToNormal(content)
{
return content.replace(/{%link (.*?) %}/g, '/persona-modding-docs/$1');
}

// Converts normal relative links to liquid link tabs
function normalLinksToLiquid(content)
{
return content.replace(/\]\((?:\/persona-modding-docs)?\/(assets\/images.*)\)/g, ']({%link $1 %})')
}

CMS.registerCustomFormat('jekyll', 'md', {
fromFile: text => fromFile(text),
toFile: value => toFile(value),
});
Loading

0 comments on commit 1d29ba2

Please sign in to comment.