Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Learning to write some pandoc.org filters
#!/usr/bin/env node
// Pandoc filter to convert first Para on each OrderedList item into a heading of the appropriate level.
'use strict';
var pandoc = require('pandoc-filter');
var HEADER_LEVELS = {
'UpperRoman': 1,
'UpperAlpha': 2,
'Decimal': 3,
'LowerAlpha': 4,
'LowerRoman': 5
}
function strip(type, x) {
var go = function (key, val) {
if (key === type) return val;
}
return pandoc.walk(x, go, '', {});
}
function create_header(level, list_item) {
var first_object = list_item[0];
var content = first_object.c;
if (first_object.t === 'Header') content = first_object.c[2];
content = strip('Strong', content);
list_item[0] = pandoc.Header(level, ["", [], []], content);
}
function action(type,value,format,meta) {
if (type === 'OrderedList') {
var current_level = HEADER_LEVELS[value[0][1].t];
var list_items = value[1];
list_items.forEach(function(current_item) {
create_header(current_level, current_item)
});
return pandoc.OrderedList(value[0], value[1]);
}
}
pandoc.stdio(action);
#!/usr/bin/env node
// Pandoc filter to split move LineBreaks outside other Inlines
'use strict';
var pandoc = require('pandoc-filter');
function action(type,value,format,meta) {
if (type === 'Span') return value[1];
if (type === 'Strong' || type === 'Emph') {
var result = [];
var lb = null;
value.forEach(function(currentValue, index, array) {
if (currentValue.t === 'LineBreak') {
lb = currentValue;
} else {
result.push(currentValue)
}
})
if (lb !== null) return [pandoc[type](result), lb];
}
}
pandoc.stdio(action);
#!/usr/bin/env node
// Pandoc filter to split Para elements on eack LineBreak
'use strict';
var pandoc = require('pandoc-filter');
function action(type,value,format,meta) {
if (type === 'Para' || type === 'Plain') {
var result = [];
var curr = [];
value.forEach(function(currentValue, index, array) {
if (currentValue.t === 'LineBreak') {
result.push(pandoc.Para(curr));
curr = []
} else {
curr.push(currentValue)
}
})
result.push(pandoc.Para(curr));
return result;
}
}
pandoc.stdio(action);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.