Skip to content

Commit

Permalink
https://github.com/cristianvasquez/obsidian-prettify/issues/85
Browse files Browse the repository at this point in the history
  • Loading branch information
cristianvasquez committed Nov 18, 2021
1 parent 624ed0d commit aa08ca6
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 61 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
"author": "Cristian",
"authorUrl": "https://github.com/cristianvasquez",
"js": "main.js",
"version": "0.1.2"
"version": "0.1.3"
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"bugs": {
"url": "https://github.com/cristianvasquez/obsidian-prettify/issues"
},
"version": "0.1.2",
"version": "0.1.3",
"description": "Prettifies your markdown!",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/__snapshots__/basic.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ exports[`Basic functionality: lists use - bullets 1`] = `
"
`;

exports[`Basic functionality: no spread 1`] = `
"- one
- two
"
`;

exports[`Embed image 1`] = `
"<https://cyberculturesblog.files.wordpress.com/2019/01/Memex.jpg>
"
Expand Down Expand Up @@ -108,3 +114,8 @@ Content
## Subheading
"
`;

exports[`Quoted test 1`] = `
"> quoted test
"
`;
125 changes: 73 additions & 52 deletions src/__tests__/basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,153 +1,174 @@
import prettifier from '../prettifier'

test("Basic functionality: a formatted table", () => {
const content = `
const content = `
**A**|**B**|**C**
|---:|:---|---|
|a |b |c|
|x |y |z|`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: lists use * bullets", () => {
const content = `
const content = `
- a
- b
- c
- d
- e
- f
`;
return prettifier(content, { bullet: "*" },{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {bullet: "*"}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: lists use - bullets", () => {
const content = `
const content = `
* a
* b
* c
* d
* e
* f
`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: alternating keeps information of different codification", () => {
const content = `
const content = `
- a
- b
* c
- d
- e
* f
`;
return prettifier(content, { bullet: "*" },{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {bullet: "*"}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: enumerate lists", () => {
const content = `
const content = `
1. foo
a. aaa
b. bbb
c. ccc
1. bar
1. baz
`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: enumerate lists", () => {
const content = `
const content = `
1. foo
1. aaa
1. bbb
4. ccc
1. bar
1. baz
`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: dont break obsidian links", () => {
const content = `[[respect this]]`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
const content = `[[respect this]]`;
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Embed youtube", () => {
const content = `
const content = `
https://www.youtube.com/watch?v=B6rKUf9DWRI
`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Embed image", () => {
const content = `
const content = `
https://cyberculturesblog.files.wordpress.com/2019/01/Memex.jpg
`;
return prettifier(content,{},{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Indent size", () => {
const content = `
const content = `
- foo
- foo
- foo
- foo
`;
return prettifier(content, { listItemIndent: "one" },{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {listItemIndent: "one"}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Indent size", () => {
const content = `
const content = `
- foo
- foo
- foo
- foo
`;
return prettifier(content, { listItemIndent: "tab" },{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {listItemIndent: "tab"}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test('Newlines around headings: enabled', () => {
const content = `
const content = `
# Heading
Content
## Subheading
`;
return prettifier(content, { newlinesAroundHeadings: true },{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {newlinesAroundHeadings: true}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Basic functionality: no spread", () => {
const content = `
* one
* two
`;
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test("Quoted test", () => {
const content = `
> quoted test
`;
return prettifier(content, {}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
});

test('Newlines around headings: disabled', () => {
const content = `
const content = `
# Heading
Content
## Subheading
`;
return prettifier(content, { newlinesAroundHeadings: false },{}).then((data) => {
expect(data.contents).toMatchSnapshot();
});
return prettifier(content, {newlinesAroundHeadings: false}, {}).then((data) => {
expect(data.contents).toMatchSnapshot();
});


});
15 changes: 15 additions & 0 deletions src/mdast-util-to-markdown-patch/handle/blockquote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = blockquote

var flow = require('../util/container-flow')
var indentLines = require('../util/indent-lines')

function blockquote(node, _, context) {
var exit = context.enter('blockquote')
var value = indentLines(flow(node, context), map)
exit()
return value
}

function map(line, index, blank) {
return '>' + (blank ? '' : ' ') + line
}
1 change: 1 addition & 0 deletions src/mdast-util-to-markdown-patch/handle/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
exports.blockquote = require('./blockquote')
exports.break = require('./break')
exports.code = require('./code')
exports.definition = require('./definition')
Expand Down
47 changes: 47 additions & 0 deletions src/mdast-util-to-markdown-patch/handle/list-item.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
module.exports = listItem

var repeat = require('repeat-string')
var checkBullet = require('../util/check-bullet')
var checkListItemIndent = require('../util/check-list-item-indent')
var flow = require('../util/container-flow')
var indentLines = require('../util/indent-lines')

function listItem(node, parent, context) {
var bullet = checkBullet(context)
var listItemIndent = checkListItemIndent(context)
var size
var value
var exit

if (parent && parent.ordered) {
bullet =
(parent.start > -1 ? parent.start : 1) +
(context.options.incrementListMarker === false
? 0
: parent.children.indexOf(node)) +
'.'
}

size = bullet.length + 1

if (
listItemIndent === 'tab' ||
(listItemIndent === 'mixed' && ((parent && parent.spread) || node.spread))
) {
size = Math.ceil(size / 4) * 4
}

exit = context.enter('listItem')
value = indentLines(flow(node, context), map)
exit()

return value

function map(line, index, blank) {
if (index) {
return (blank ? '' : repeat(' ', size)) + line
}

return (blank ? bullet : bullet + repeat(' ', size - bullet.length)) + line
}
}
14 changes: 7 additions & 7 deletions src/mdast-util-to-markdown-patch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var defaultHandlers = require('./handle')
var defaultJoin = require('./join')
var defaultUnsafe = []

function toMarkdown(tree, options) {
function toMarkdown (tree, options) {
var settings = options || {}
var context = {
enter: enter,
Expand Down Expand Up @@ -35,7 +35,7 @@ function toMarkdown(tree, options) {
handlers: context.handlers
})

result = context.handle(tree, null, context, {before: '\n', after: '\n'})
result = context.handle(tree, null, context, { before: '\n', after: '\n' })

if (
result &&
Expand All @@ -47,25 +47,25 @@ function toMarkdown(tree, options) {

return result

function enter(name) {
function enter (name) {
context.stack.push(name)
return exit

function exit() {
function exit () {
context.stack.pop()
}
}
}

function invalid(value) {
function invalid (value) {
throw new Error('Cannot handle value `' + value + '`, expected node')
}

function unknown(node) {
function unknown (node) {
throw new Error('Cannot handle unknown node `' + node.type + '`')
}

function joinDefinition(left, right) {
function joinDefinition (left, right) {
// No blank line between adjacent definitions.
if (left.type === 'definition' && left.type === right.type) {
return 0
Expand Down
1 change: 1 addition & 0 deletions src/prettifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function prettifier(
];
}

//https://github.com/cristianvasquez/obsidian-prettify/issues/19
stringifyOptions.handlers = {
listItem: listItem
}
Expand Down

0 comments on commit aa08ca6

Please sign in to comment.